How do you round a number to two decimal places in C#?

后端 未结 15 1249
挽巷
挽巷 2020-11-22 08:59

I want to do this using the Math.Round function

15条回答
  •  醉梦人生
    2020-11-22 09:43

    Wikipedia has a nice page on rounding in general.

    All .NET (managed) languages can use any of the common language run time's (the CLR) rounding mechanisms. For example, the Math.Round() (as mentioned above) method allows the developer to specify the type of rounding (Round-to-even or Away-from-zero). The Convert.ToInt32() method and its variations use round-to-even. The Ceiling() and Floor() methods are related.

    You can round with custom numeric formatting as well.

    Note that Decimal.Round() uses a different method than Math.Round();

    Here is a useful post on the banker's rounding algorithm. See one of Raymond's humorous posts here about rounding...

提交回复
热议问题