C#float.ToString舍入值 [英] C# float.ToString Rounding Values
问题描述
也许我错过了一些东西,但是 float.ToString()
方法将数字舍去了数字,这使我很头痛。
在以下小代码中:
当输入12345678作为输入时,调试器中的浮点数是正确的,但 ToString
方法的输出是12345680(任何格式我试过...)
$ p $
Console.WriteLine(请输入一个浮点数);
float theFloat = float.Parse(Console.ReadLine());
Console.WriteLine(string.Format(You have entered {0},theFloat.ToString(F)));
并输出:
请输入浮点数
12345678
您已输入12345680.00
帮助将不胜感激!
从float的文档:
您的电话号码( 12345678
)长度超过了8位,所以默认 到7位有效数字,产生 12345680
。 (注意默认。)
然而,尽管微软的文章中提到了浮点数的精确度,但事实上, 9位精度。
Single.ToString() 的Microsoft文档声明:
< blockquote>
默认情况下,返回值只包含精确度的7位数字,但内部最多保留9位数字。
然后继续说:
lockquote
如果你需要更高的精度,用G9格式指定格式总是返回9位数的精度或R,如果数字可以用这个精度表示,则返回7位;如果数字只能用最大精度表示,则返回9位数字。
有了这个信息,我们可以写下这个代码:
Console.WriteLine(12345678f.ToString(G9));
确实可以打印 12345678
p>
我无法解释的是为什么微软声明一个浮点数有7个数字的精度,然后继续让我们使用9位数字...
但是,请注意,并非所有8位(或9位)十进制数字整数都将在浮点数中具有精确的表示形式,如下面的代码所示(最后一位数字不同): b
$ b
Console.WriteLine(16777217f.ToString(R)); //打印16777216
Maybe I am missing something but the float.ToString()
method rounds numbers, which causes me a lot of headache.
Take a look at the following small code:
When entering 12345678 as input, the float number in the debugger is correct but the output of the ToString
methods is 12345680 (in any format I've tried...)
Console.WriteLine("Please enter a float number");
float theFloat = float.Parse(Console.ReadLine());
Console.WriteLine(string.Format("You have entered {0}", theFloat.ToString("F")));
And the output:
Please enter a float number
12345678
You have entered 12345680.00
Help will be much appreciated!
From the documentation for float:
- The range of a float is -3.4 × 10^38 to +3.4 × 10^38
- The precision of a float is 7 digits.
Your number, 12345678
, at 8 digits long exceeds the precision, so it is by default being rounded to 7 significant digits, which yields 12345680
. (Note the by default.)
However, despite what that Microsoft article says about the precision of a float, in reality it holds up to 9 digits of precision.
The Microsoft documentation for Single.ToString()
states:
By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally.
It then goes on to say:
If you require more precision, specify format with the "G9" format specification, which always returns 9 digits of precision, or "R", which returns 7 digits if the number can be represented with that precision or 9 digits if the number can only be represented with maximum precision.
Armed with this information, we can write this code:
Console.WriteLine(12345678f.ToString("G9"));
Which does indeed print 12345678
.
What I can't explain is why Microsoft state that a float has 7 digits of precision, and then goes on to let us use 9 digits...
However, note that not all 8 (or 9) decimal digit integers will have an exact representation in a float, as the following code demonstrates (the last digit differs):
Console.WriteLine(16777217f.ToString("R")); // Prints 16777216
这篇关于C#float.ToString舍入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!