Friday, April 9, 2010

How to Format Decimal numbers in C# and remove extra zero digits

Sometimes you want to show the decimal numbers and you do not want to show the extra zero digits after decimal point.
for below code, if there are one or two digits to the right of the decimal point, that shows up, but, if there are digits of zero to the right of the decimal point, the number is formatted as a whole number

decimal d = 2.340M;
string result = string.Format("{0:0.##}",d); // result will be 2.34

Cheers!

0 comments:

Post a Comment