Monday, November 22, 2010

C#.NET: How to format a decimal number to show thousand separator according to UI Culture

See following sample which trying to format sample number 25400.

decimal decimalNumber = 25400;
string number = (decimalNumber).ToString("N0", CultureInfo.CurrentUICulture.NumberFormat);

after formatting the number looks like follows 25,400(Here my UI Culture is en-US and the thousand separator may vary according to the user's UI culture)

If you need to show two decimal points then use following code.
string number = (decimalNumber).ToString("N2", CultureInfo.CurrentUICulture.NumberFormat);

Then the output will looks like 25,400.00

Like wise you can use "N3", "N4" etc. to change the allowed number of decimal points.

0 comments:

Post a Comment