<分区>
Possible Duplicate:
Why does .NET use banker's rounding as default?
这是一个示例代码
decimal num1=390, num2=60, result;
result=num1/num2; // here I get 6.5
result=Math.Round(result,0);
结果的最终值应该是7,但是我得到的是6。为什么会有这样的行为?
<分区>
Possible Duplicate:
Why does .NET use banker's rounding as default?
这是一个示例代码
decimal num1=390, num2=60, result;
result=num1/num2; // here I get 6.5
result=Math.Round(result,0);
结果的最终值应该是7,但是我得到的是6。为什么会有这样的行为?
最佳答案
检查第三个参数MidpointRounding .
默认使用MidpointRounding.ToEven,所以
Math.Round(result,0); // 6.0
//or
Math.Round(result,0, MidpointRounding.ToEven); // 6.0
//But:
Math.Round(result,0, MidpointRounding.AwayFromZero); // 7.0
关于c# - C# 中小数的 Math.Round,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4468841/