Why does Math.Round(2.5) return 2 instead of 3?

前端 未结 15 2109
灰色年华
灰色年华 2020-11-22 03:54

In C#, the result of Math.Round(2.5) is 2.

It is supposed to be 3, isn\'t it? Why is it 2 instead in C#?

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 04:41

    using a custom rounding

    public int Round(double value)
    {
        double decimalpoints = Math.Abs(value - Math.Floor(value));
        if (decimalpoints > 0.5)
            return (int)Math.Round(value);
        else
            return (int)Math.Floor(value);
    }
    

提交回复
热议问题