String.Format() for floating point to display fixed number of places?
If I want to display, say, 4 decimal po开发者_运维技巧ints, what is the correct format?
String.Format("{0:0.0000}", floatNum);
That will always display four decimal places, regardless of what the value is. Other options can be found here: http://www.csharp-examples.net/string-format-double/
Personally, I prefer this approach.
floatNum.ToString("N4")
Notice that it rounds:
decimal d = 1.23456789M;
Console.WriteLine(d.ToString("0.0000"));
// Output: 1.2345
As a format string, it would be:
Console.WriteLine("{0:0.0000}", d);
精彩评论