ToEven或远离零的数学回合问题 [英] Math Round problem with ToEven or Away From Zero

查看:171
本文介绍了ToEven或远离零的数学回合问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







根据msdn对舍入方法的辩护:



----------------------------------------------- ------------------------------------------------ <无线电通信/>
ToEven - 当一个数字在另外两个数字之间时,它会向最接近的偶数舍入。

AwayFromZero - 当一个数字在另外两个数字之间时,它会向四舍五入最近的数字远离零。

---------------------------------- -------------------------------------------------- -----------



但是当我尝试使用Math.Round时,我得到了以下结果:



double x1 = Math.Round(2.1125,3,MidpointRounding.ToEven); //结果2.112

double x2 = Math.Round(2.1125,3,MidpointRounding.AwayFromZero); //结果2.113

double x3 = Math.Round(2.1135,3,MidpointRounding.ToEven); //结果2.114

double x4 = Math.Round(2.1135,3,MidpointRounding.AwayFromZero); //结果2.114



double y1 = Math.Round(2.125,2,MidpointRounding.ToEven); //结果2.12

double y2 = Math.Round(2.125,2,MidpointRounding.AwayFromZero); //结果2.13

double y3 = Math.Round(2.135,2,MidpointRounding.ToEven); //结果2.13

double y4 = Math.Round(2.135,2,MidpointRounding.AwayFromZero); //结果2.13



当舍入3位小数时,结果是预期的。

但是当舍入2位小数时,我得到错误的结果。 />


y3应该返回2.14因为我们正在向EVEN数字四舍五入。

y4应该返回2.14,因为我们正在从零开始。



我不明白结果。有人可以向我解释一下吗?







Eldad。

解决方案

请参阅MSDN关于此方法的页面上的调用者注释: Math.Round(Double,Int32,MidpointRounding) [ ^ ]:由于将十进制值表示为浮点数或对浮点值执行算术运算可能导致精度损失,在某些情况下为Round(Double) ,Int32,MidpointRounding)方法可能看起来不像模式参数指定的圆中点值。...

问候,

Ian。

Hi,


According to msdn defenition for rounding methods :

-----------------------------------------------------------------------------------------------
ToEven - When a number is halfway between two others, it is rounded toward the nearest even number.
AwayFromZero - When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
-----------------------------------------------------------------------------------------------

however when trying to use Math.Round i get these results :

double x1 = Math.Round(2.1125, 3, MidpointRounding.ToEven); // Result 2.112
double x2 = Math.Round(2.1125, 3, MidpointRounding.AwayFromZero); // Result 2.113
double x3 = Math.Round(2.1135, 3, MidpointRounding.ToEven); // Result 2.114
double x4 = Math.Round(2.1135, 3, MidpointRounding.AwayFromZero); // Result 2.114

double y1 = Math.Round(2.125, 2, MidpointRounding.ToEven); // Result 2.12
double y2 = Math.Round(2.125, 2, MidpointRounding.AwayFromZero); // Result 2.13
double y3 = Math.Round(2.135, 2, MidpointRounding.ToEven); // Result 2.13
double y4 = Math.Round(2.135, 2, MidpointRounding.AwayFromZero); // Result 2.13

when rounding 3 decimals the result is expected.
but when rounding 2 decimals I get a wrong result.

y3 should return 2.14 because we are rounding to EVEN numbers.
y4 should return 2.14 becuase we are rounding away from zero.

I dont understand the results. Can anyone please explain them to me ?



Eldad.

解决方案

See the "Notes to Callers" on MSDN''s page about this method: Math.Round(Double, Int32, MidpointRounding)[^]: "Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double, Int32, MidpointRounding) method may not appear to round midpoint values as specified by the mode parameter. ..."
Regards,
Ian.


这篇关于ToEven或远离零的数学回合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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