C# Numeric ("N") Format Specifier

Description

The numeric ("N") specifier formats a number to a string of the form "-d,ddd,ddd.ddd...", where

  • "-" indicates a negative,
  • "d" indicates a digit (0-9),
  • "," indicates a group separator, and
  • "." indicates a decimal point symbol.
Item Value
Name Number
Format specifier "N" or "n"
Supported by All numeric types.
ResultIntegral and decimal digits, group separators, and a decimal separator with optional negative sign.
Precision specifier indicates the desired number of digits after the decimal point.
Default precision specifierDefined by System.Globalization.NumberFormatInfo.

Example,

Value FormatFormated
1234.567 ("N", en-US) 1,234.57
1234.567 ("N", ru-RU)1 234,57
1234 ("N1", en-US)1,234.0
1234 ("N1", ru-RU)1 234,0
-1234.56 ("N3", en-US)-1,234.560
-1234.56 ("N3", ru-RU) -1 234,560

The result string is affected by the formatting NumberFormatInfo properties.

  • NegativeSign
  • NumberNegativePattern
  • NumberGroupSizes
  • NumberGroupSeparator
  • NumberDecimalSeparator
  • NumberDecimalDigits

Example


using System;/* w  w  w. ja  va  2s.  c  om*/
using System.Globalization;
class MainClass
{
   static void Main()
   {
        double dblValue = -12445.6789;
        Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
        // Displays -12,445.68
        Console.WriteLine(dblValue.ToString("N1", 
                          CultureInfo.CreateSpecificCulture("sv-SE")));
        // Displays -12 445,7 
        
        int intValue = 123456789;
        Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
        // Displays 123,456,789.0 
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var