c# - C# 中小数的 Math.Round

标签 c# .net math

<分区>

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/

相关文章:

c# - 将数字字符串扩展为单个数字

c# - 使用 watin 搜索级联元素

c# - Entity Framework 迁移重命名表和列

c# - 开发人员如何使用源代码控制,我试图找到在小型开发环境中进行源代码控制的最有效方法

math - Maple 中的多项式回归

c#在for循环中更改控件的名称

.net - 在 TFS 构建上创建 Azure 包

.net - 这是 WPF 数据网格错误吗?

math - 在 3D 空间中旋转矢量

math - float 学有问题吗?