String.Format() for floating point to display fixed number of places?
-
20-09-2019 - |
Question
If I want to display, say, 4 decimal points, what is the correct format?
Solution
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/
OTHER TIPS
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow