Unlike everyone suggesting using the G format specifier, I would suggest the following to keep both a thousand separators and a decimal point when deleting extra zeros:
{0:
The result of this format is in most cases much better than G:
String.Format("{0:#,#.##}",25/2.4); 10.42 String.Format("{0:#,#.##}",1000000); 1,000,000 String.Format("{0:#,#.##}",1000000.3600000); 1,000,000.36
And the G specifier cannot handle all possible combinations:
String.Format("{0:G29}",25/2.4); 10.416666666666668 String.Format("{0:G2}",25/2.4); 10 String.Format("{0:G29}",1000000.3600000); 1000000.36 String.Format("{0:G2}",1000000.3600000); 1E+06
Mohsen Afshin Apr 30 '15 at 7:16 2015-04-30 07:16
source share