Sorry for reactivating this question, but I didn’t find the right answer here.
In formatting numbers you can use 0 as a mandatory place and # as an optional place.
So:
// just two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
You can also combine 0 with #.
String.Format("{0:0.0#}", 123.4567) // "123.46"
String.Format("{0:0.0#}", 123.4) // "123.4"
String.Format("{0:0.0#}", 123.0) // "123.0"
For this formating method is always used CurrentCulture. For some Cultures . will be changed to ,.
Answer to original question:
The simpliest solution comes from @Andrew (here). So I personally would use something like this:
var number = 123.46;
String.Format(number % 1 == 0 ? "{0:0}" : "{0:0.00}", number)