Format Numbers in C# using inbuilt format Specifier.

C# allows several inbuilt formatting specifiers which can be used to quickly format a number, for example the ‘c' specifier will format the number as a currency.

string outputFormat = "";

double dblvalue = 9999.9999999;

outputFormat = string.Format("{0:c}", dblvalue);

MessageBox.Show(outputFormat);

// output will be $10,000.00

Some more specifiers are,

c – Currency
d – Decimal
e – Exponential notation
f – Fixed point formatting
n – Numerical formatting (with commas)
x – Hexidecimal

examples:-

outputFormat = string.Format("{0:e}", dblvalue); //1.000000e+004

outputFormat = string.Format("{0:f}", dblvalue); //10000.00

outputFormat = string.Format("{0:n}", dblvalue); //10,000.00

Thank You.