净四舍五入问题 [英] .Net Rounding Issue

查看:206
本文介绍了净四舍五入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有净内部的四舍五入问题。

I have a rounding issue inside of .Net.

我四舍五入3位数字下降到两位数字,它会引起一些问题,一个数字。

I am rounding a 3 digit number down to two digits and it is causing some problems with one number.

如果我试图轮34.425到小数点后两位,应该是四舍五入至34.43。我现在用的是roundawayfromzero选项,它在程序中,除了这一个至今就职于每个数字。

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.

在code Math.Round(34.425,2,MidpointRounding.AwayFromZero)但是应该等于34.43,它等于34.42。

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?

有关,现在我已经通过数转换为十进制解决了这个问题。我已经改变了code到这一点,它工作正常现在:

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

我只是在寻找为什么我的旧code没有工作的理由。

I am just looking for a reason on why my old code did not work.

感谢您!

更新了code为正确AwayFromZero

Updated the code to the correct AwayFromZero

推荐答案

浮点从来都不是确切的,34.425可能有一个内部的重新presantation 34.4249999999999 ..这将四舍五入至34.42。

Floating point is never exact, 34.425 may have an internal represantation 34.4249999999999.. which will be rounded up to 34.42.

如果你需要精确的再presentation换号使用十进制键入

If you need exact representation for numbers use the decimal type.

这篇关于净四舍五入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆