Introduction
The standard numeric Format Specifiers can be used to convert numbers to strings in various formats. There are various formats in which a number can be converted; the list of format types is:
Number ( "N") Format Specifier
Decimal ("D") Format Specifier
Exponential ("E") Format Specifier
Percent ("P") Format Specifier
Fixed-Point ("F") Format Specifier
Number Format Specifier
This Format Specifier can be used to convert a number into a string of the form "-d,ddd,ddd.ddd....." where d represents any number between 0 to 9. The negative sign represents a negative number and "." denotes the decimal point in the number and "," denotes the group separator.
namespace NumberFormatting
{
class Program
{
static void Main(string[] args)
{
double dblValue = -12445.6789;
Console.WriteLine("The double value is as : " + dblValue);
Console.WriteLine("The Number Formatting is as :"+ dblValue.ToString("N", CultureInfo.InvariantCulture));
}
}
}
Output
Decimal Format Specifier
This decimal Format Specifier string can be used to convert a number into the decimal format string as in:
namespace NumberFormatting
{
class Program
{
static void Main(string[] args)
{
int value= 12345;
Console.WriteLine("The value is :" + value);
Console.WriteLine("The decimal format string is :"+ value.ToString("D"));
Console.WriteLine("The decimal format string with 8 number digits is as :"+ value.ToString("D8"));
}
}
}
Output
Exponential Format Specifier
The exponential ("E") Format Specifier converts a number to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative. See:




Join the conversation! Your thoughts help the community grow.