gpt4 book ai didi

C# - 将十进制转换为 int32

转载 作者:可可西里 更新时间:2023-11-01 08:15:20 32 4
gpt4 key购买 nike

我有以下代码:

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com