C# - (INT)Math.Round((双)(3514 + 3515)/ 2)= 3514? [英] C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?

查看:185
本文介绍了C# - (INT)Math.Round((双)(3514 + 3515)/ 2)= 3514?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直升机大家。

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?

Returns 3514; should not be 3515?

推荐答案

Math.Round是罪魁祸首

Math.Round is the culprit

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

它就是我们所说的银行家舍入,甚至舍入。

Its what we call Banker's Rounding or even rounding.

在Math.Round信息说:

Info on Math.Round says

最近的一个整数。如果一个小数部分是半两个整数,其中之一是,即使与其他奇之间,则偶数被返回。

3514.5舍入为3514和3515.5还将四舍五入至3514

3514.5 is rounded to 3514 and 3515.5 will also be rounded to 3514.

读<一个href=\"http://stackoverflow.com/questions/977796/why-does-math-round2-5-return-2-instead-of-3-in-c\">this

要避免这样做。

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

这篇关于C# - (INT)Math.Round((双)(3514 + 3515)/ 2)= 3514?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆