How might I convert a double to the nearest integer value?

后端 未结 8 1947
傲寒
傲寒 2020-12-08 08:45

How do you convert a double into the nearest int?

8条回答
  •  星月不相逢
    2020-12-08 09:35

    Use Math.round(), possibly in conjunction with MidpointRounding.AwayFromZero

    eg:

    Math.Round(1.2) ==> 1
    Math.Round(1.5) ==> 2
    Math.Round(2.5) ==> 2
    Math.Round(2.5, MidpointRounding.AwayFromZero) ==> 3
    

提交回复
热议问题