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

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

How do you convert a double into the nearest int?

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 09:28

    double d = 1.234;
    int i = Convert.ToInt32(d);
    

    Reference

    Handles rounding like so:

    rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

提交回复
热议问题