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

前端 未结 2 611
北海茫月
北海茫月 2021-01-27 04:18

Helo everyone.

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

    void average1()
    {
        List aveList = new List { ai1[0],ai1[1]};
        dou         


        
2条回答
  •  情歌与酒
    2021-01-27 04:35

    Math.Round is the culprit

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

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

    Info on Math.Round says

    The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned.

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

    Read this

    To avoid do this

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

提交回复
热议问题