的String.Format()的浮点显示的地方固定数量?
-
20-09-2019 - |
题
如果我想显示,比方说,4个小数点,什么是正确的格式?
解决方案
String.Format("{0:0.0000}", floatNum);
这将始终显示四位小数不管值是什么。其他选项可以在这里找到: http://www.csharp-examples.net/string -format双/
其他提示
就个人而言,我更喜欢这种方法。
floatNum.ToString("N4")
请注意,其四舍五入:
decimal d = 1.23456789M;
Console.WriteLine(d.ToString("0.0000"));
// Output: 1.2345
作为格式串,这将是:
Console.WriteLine("{0:0.0000}", d);
不隶属于 StackOverflow