Math.round(-1.5)
结果是-1
,但是为什么是-1
网上居然众说纷纭,其实只需要看一看源码的javadoc就知道了(jdk11):
/**
* Returns the closest {@code long} to the argument, with ties
* rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
* <li>If the argument is negative infinity or any value less than or
* equal to the value of {@code Long.MIN_VALUE}, the result is
* equal to the value of {@code Long.MIN_VALUE}.
* <li>If the argument is positive infinity or any value greater than or
* equal to the value of {@code Long.MAX_VALUE}, the result is
* equal to the value of {@code Long.MAX_VALUE}.</ul>
*
* @param a a floating-point value to be rounded to a
* {@code long}.
* @return the