我有以下代码:
int a = Convert.ToInt32(4.5m);
int b = Convert.ToInt32(5.5m);
Console.WriteLine(a);
Console.WriteLine(b);
这是输出:
4
6
为什么 Convert.ToInt32
将十进制值舍入到最接近的偶数?
最佳答案
Convert 使用四舍五入或银行四舍五入:
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.
To control the type of rounding used by the Round method, call the Math.Round(Double, MidpointRounding) overload.
关于C# - 将十进制转换为 int32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549912/