gpt4 book ai didi

C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:27 25 4
gpt4 key购买 nike

大家好。

int[] ai1=new int[2] { 3514,3515 };

void average1()
{
List<int> aveList = new List<int> { ai1[0],ai1[1]};
double AveragLI = aveList.Average();
int AverLI = (int)Math.Round((double)AveragLI);
label1.Text = AverLI.ToString();
}

返回 3514;不应该是3515吗?

最佳答案

Math.Round 是罪魁祸首

int AverLI = (int)Math.Round((double)AveragLI);

这就是我们所说的 Banker's Rounding 甚至四舍五入。

关于 Math.Round 的信息说

最接近a的整数。如果 a 的小数部分介于两个整数之间,其中一个为偶数,另一个为奇数,则返回偶数。

3514.5 四舍五入为 3514,3515.5 也四舍五入为 3514。

阅读this

为了避免这样做

int AverLI = (int)Math.Ceiling((double)AveragLI);

关于C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369664/

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