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

前端 未结 15 1992
灰色年华
灰色年华 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:28

    From MSDN:

    By default, Math.Round uses MidpointRounding.ToEven. Most people are not familiar with "rounding to even" as the alternative, "rounding away from zero" is more commonly taught in school. .NET defaults to "Rounding to even" as it is statistically superior because it doesn't share the tendency of "rounding away from zero" to round up slightly more often than it rounds down (assuming the numbers being rounded tend to be positive.)

    http://msdn.microsoft.com/en-us/library/system.math.round.aspx

提交回复
热议问题