Introduction
The numeric format specifier "N" converts a number to a string format as, "-d,ddd,ddd.ddd…" where the "-" sign denotes the negative number symbol, "," denotes a separator between these groups, "d" denotes any number (0-9), and "." represents the decimal point in the number. For this we use the properties of the NumberFormatInfo class. First of all the following example shows how we convert the number into the string format:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
//define a double type number
double number = -98765.6789;
//convert double number into a string
Console.WriteLine(" The double group number is:"+number.ToString("N", CultureInfo.InvariantCulture));
//define the integer type number
int intValue = 123456789;
//convert integer number into the string
Console.WriteLine("The integer grouped number is :"+intValue.ToString("N1", CultureInfo.InvariantCulture));
}
}
}
Output
The various properties with their examples are as follows.
NumberNegativePattern
This property is used to define the format of negative values, and specifies whether the negative sign is represented by parentheses or the NegativeSign property, or we can say that it gets or sets the format pattern for negative numeric values. This property has one of the following values:
Value |
Pattern |
0 |
(n) |
1 |
-n |
2 |
- n |
3 |
n- |
4 |
n - |
Here n denotes the number and "-" denotes the negative sign. For example:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
// Creates a object of NumberFormatinfo class
NumberFormatInfo number = new NumberFormatInfo();
// Initialize a negative value.
Int64 myInt = -1234;
// The default formatting of the number is displayed here
Console.WriteLine("Default Formatting of the Number is as \t:\t{0}", myInt.ToString("N", number));
// Displays the value with other patterns.
for (int i = 0; i <= 4; i++)
{
number.NumberNegativePattern = i;
Console.WriteLine("New Pattern with value {0}\t:\t{1}", number.NumberNegativePattern, myInt.ToString("N", number));
}
}
}
}
Output
NumberGroupSizes
This property defines the number of integral digits that appears between group separators. We can also say that it gets or sets the number of digits in each group to the left of the decimal in numeric values. For example:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
// Gets a NumberFormatInfo associated with the en-US culture.
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
// Intialize a integer value
Int64 number = 123456789012345;
//Display the default numeric value
Console.WriteLine("Default Number is as :"+number.ToString("N", nfi));
// Displays the same value with different groupings.
//sizes1 denotes an array has sizes 2,3 and 4
int[] Sizes1 = { 2, 3, 4 };
//sizes2 denotes an array has sizes 2,3 and 0
int[] Sizes2 = { 2, 3, 0 };
nfi.NumberGroupSizes = Sizes1;
Console.WriteLine("After apply size 1 In number Format :"+number.ToString("N", nfi));
nfi.NumberGroupSizes = Sizes2;
Console.WriteLine("After apply the size 2 In number format :"+number.ToString("N", nfi));
}
}
}
Here Size1 applies 2, 3 and 4 sizes to the number, in other words now the grouping begins from right side of the number after the decimal point and the first two digits will be grouped then 3 digits then 4 digits after that if digits are left all are grouped by 4 digit numbers, the same as Size2.
Output
NumberGroupSeparator
This property is used to separate the number by grouping. In other words, if the number is grouped (apply comma in this) then this property separates a number where the comma exists, or we can say that it gets or sets the string that separates groups of digits to the left of the decimal in numeric values. For example:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
// Gets a NumberFormatInfo with the en-US culture.
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
// Displays a value with the default separator (",").
Int64 myInt = 123456789;
Console.WriteLine("Grouped Value : "+myInt.ToString("N", nfi));
// Displays the same value when apply separator.
nfi.NumberGroupSeparator = " ";
Console.WriteLine("Seperated Value :"+myInt.ToString("N", nfi));
}
}
}
Output
NumberDecimalSeparator
This property will separate the number after the decimal point with the number that exists before the decimal point Or we can say that it gets or sets the string to use as the decimal separator in numeric values. For example:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
// Gets a NumberFormatInfo with the en-US culture.
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
// Displays a value with the default separator (",").
Int64 myInt = 123456789;
Console.WriteLine("Grouped Value : "+myInt.ToString("N", nfi));
// Displays the same value when apply decimal separator.
nfi.NumberDecimalSeparator = " ";
Console.WriteLine("Decimal Seperated Value :"+myInt.ToString("N", nfi));
}
}
}
Output
NumberDecimalDigit
This property is used to get and set the number of decimal places in the grouped number format. For example:
namespace NumericFunction
{
class Program
{
static void Main(string[] args)
{
// Gets a NumberFormatInfo with the en-US culture.
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
// Displays a value with the default separator (",").
Int64 myInt = 123456789;
Console.WriteLine("Grouped Value : "+myInt.ToString("N", nfi));
// Displays the same value when apply number decimal digits
nfi.NumberDecimalDigits = 5;
Console.WriteLine("Number Decimal Value :"+myInt.ToString("N", nfi));
}
}
}
Output
Summary
In this article I explained how to use the number format specifier to perform various changes in the number format string in C#.