Why does .NET use banker's rounding as default?

前端 未结 5 1322
一向
一向 2020-11-22 06:30

According to the documentation, the decimal.Round method uses a round-to-even algorithm which is not common for most applications. So I always end up writing a custom functi

5条回答
  •  花落未央
    2020-11-22 06:50

    Use another overload of Round function like this:

    decimal.Round(2.5m, 0,MidpointRounding.AwayFromZero)
    

    It will output 3. And if you use

    decimal.Round(2.5m, 0,MidpointRounding.ToEven)
    

    you will get banker's rounding.

提交回复
热议问题