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

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

How do you convert a double into the nearest int?

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 09:32

    You can also use function:

    //Works with negative numbers now
    static int MyRound(double d) {
      if (d < 0) {
        return (int)(d - 0.5);
      }
      return (int)(d + 0.5);
    }
    

    Depending on the architecture it is several times faster.

提交回复
热议问题