Rounding negative numbers in Java

后端 未结 6 942
予麋鹿
予麋鹿 2020-12-09 10:30

According to Wikipedia when rounding a negative number, you round the absolute number. So by that reasoning, -3.5 would be rounded to -4. But when I use java.lang.Math.round

6条回答
  •  春和景丽
    2020-12-09 10:54

    According to Javadocs:

    Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

    (long)Math.floor(a + 0.5d)

提交回复
热议问题