C# - 将十进制转换为 int32

标签 c# .net math rounding

我有以下代码:

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/

相关文章:

math - 在软件工程中,是否要求你擅长数学或物理?

opencv - 使用SVD时,如何对Sigma对角线的比例因子进行分类?

java - 以贪婪的方式分配给定的数字

c# - 以编程方式访问 MSDN 类和文档?

c# - "ReorderListBoxItem must have a DragHandle ContentPresenter part."问题

c# - public 类名[][] 方法名{ get;放;这在 C# 中意味着什么?

.net - 如何分别用delphi和.Net生成Winform应用程序?

c# - 插值字符串格式问题

c# - SignalR C# MVC 将匿名用户映射到客户端 ID

c# - C#中用RSAServiceProvider加解密