.Net 舍入问题
我在 .Net 内部遇到舍入问题。
我将一个 3 位数四舍五入为两位数,这导致一个数字出现一些问题。
如果我尝试将 34.425 四舍五入到小数点后两位,它应该四舍五入到 34.43。我正在使用 roundawayfromzero 选项,到目前为止,它对程序中的每个数字都有效,除了这个数字。
代码 Math.Round(34.425, 2, MidpointRounding.AwayFromZero)
应等于 34.43,但是,它等于 34.42。
如果我用任何其他数字尝试这个,效果都很好。
Math.Round(34.435, 2, MidpointRounding.AwayFromZero) = 34.44
Math.Round(34.225, 2, MidpointRounding.AwayFromZero) = 34.23
Math.Round(34.465, 2, MidpointRounding.AwayFromZero) = 34.47
我只是想看看以前是否有人遇到过这个问题?
现在我已经通过将数字转换为小数来解决这个问题。我已将代码更改为此,现在工作正常:
Math.Round(CDec(34.425), 2, MidpointRounding.AwayFromZero) = 34.43
我只是在寻找旧代码不起作用的原因。
谢谢你!
将代码更新为正确的 AwayFromZero
I have a rounding issue inside of .Net.
I am rounding a 3 digit number down to two digits and it is causing some problems with one number.
If I try to round 34.425 to two decimal places it should round it to 34.43. I am using the roundawayfromzero option and it has worked for every number in the program except for this one so far.
The code Math.Round(34.425, 2, MidpointRounding.AwayFromZero)
should equal 34.43 however, it equals 34.42.
If I try this with any other number it works fine.
Math.Round(34.435, 2, MidpointRounding.AwayFromZero) = 34.44
Math.Round(34.225, 2, MidpointRounding.AwayFromZero) = 34.23
Math.Round(34.465, 2, MidpointRounding.AwayFromZero) = 34.47
I just wanted to check to see if anyone has run into this problem before?
For right now I have fixed this problem by converting the number to a decimal. I have changed the code to this and it works fine now:
Math.Round(CDec(34.425), 2, MidpointRounding.AwayFromZero) = 34.43
I am just looking for a reason on why my old code did not work.
Thank you!
Updated the code to the correct AwayFromZero
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
浮点从来都不是精确的,34.425 可能有一个内部表示 34.4249999999999.. 它将被舍入到 34.42。
如果您需要数字的精确表示,请使用
decimal
类型。Floating point is never exact, 34.425 may have an internal represantation 34.4249999999999.. which will be rounded up to 34.42.
If you need exact representation for numbers use the
decimal
type.有点困惑您实际上是在使用
MidpointRounding.ToEven
还是MidpointRounding.AwayFromZero
。如果您使用ToEven
(如第一个代码段所示),则这是预期的行为。Slightly confused about whether you're actually using
MidpointRounding.ToEven
orMidpointRounding.AwayFromZero
. If you are usingToEven
as the first snippet indicates, this is expected behavior.你的假设和结果是不正确的:
这
就是它的工作原理,这就是我在盒子上得到的。舍入到偶数就是指向上或向下舍入到感兴趣的小数点处的下一个偶数。
Your assumptions and results are incorrect:
and
That's how it works, and that's what I get on my box. Rounding to even means just that, rounding up or down to get to the next even number at the decimal place of interest.
为什么? ToEven 必须使其等于 34.42,因为 42 是偶数。行为是正确的。
Why? ToEven must make it 34.42 since 42 is even. Behaviour is correct.
我不使用 vb.net,所以我的推理可能不正确,但根据参数 MidpointRounding.ToEven 的名称,我希望 34.425 四舍五入为 34.42;但我还希望 34.225 舍入为 34.22,34.465 舍入为 34.46。在您的示例中,以 5 结尾的数字的四舍五入是一个约定问题。最常见的约定是舍入为偶数,我推断您将采用 .ToEven 参数。
此外,我怀疑您遇到了二进制/十进制舍入问题。检查 34.425 的二进制表示形式为 10010.011011....... 当您考虑十进制数(字节、字、双字、四字和负/正补码)的计算机存储和表示形式时,这可能会导致你没有四舍五入你认为的数字。
为了获得您想要的结果,您可能应该在四舍五入之前将 0.00001 添加到任何以 5 结尾的数字。
I don't use vb.net, so my reasoning may be incorrect, but based on the names of your parameters MidpointRounding.ToEven, I would expect 34.425 to be rounded to 34.42; but I would also expect 34.225 to be rounded to 34.22 and 34.465 to 34.46. The rounding of numbers that end in 5 as in your examples is a matter of convention. The most usual convention is round to an even number, which I deduce your .ToEven parameter would be adopting.
In addition, I suspect you are encountering the binary/decimal rounding problem. Checking the binary representation of 34.425 comes out as 10010.011011....... When you take the computer storage and representation of a decimal number (byte, word, double word, quad word and negative/positive complement) into account this can result in you not rounding the number you thought you were.
To get the results you want, you should probably add 0.00001 to any digit ending in 5 before rounding.