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

前端 未结 2 641
北海茫月
北海茫月 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:25

    The default rounding scheme for Math.Round is what's known as banker's rounding (which is the standard in financial and statistical areas), where midpoint values are rounded to the nearest even number. It looks like you were expecting midpoint values to be rounded away from zero (which is the kind you were probably taught in grade school: if it ends in 5, round up).

    If you were just concerned that it wasn't working in an acceptable way, don't worry. If you'd like it to be rounded away from zero, you can do this:

    int AverLI = (int)Math.Round((double)AveragLI, MidpointRounding.AwayFromZero);
    

提交回复
热议问题