首页 > 代码库 > Java语言中小数的取整
Java语言中小数的取整
1、double java.lang.Math.floor(double a)
返回最大的double值(在正无穷方向上最接近的)小于或等于参数a的一个数学意义上的整数,特例:
如果参数值等于数学上的整数,则返回结果与参数值相同
如果参数是NaN(非数值)或是无穷或是+0或是-0,则返回值和参数值相同
Returns the largest (closest to positive infinity) double
value that is less than or equal to the argument and is equal to a mathematical integer. Special cases:
If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
Returns the smallest (closest to negativeinfinity) double
value that is greater than or equalto the argument and is equal to a mathematical integer. Specialcases:
- If the argument value is already equal toa mathematical integer, then the result is the same as theargument.
- If the argument is NaN or an infinity orpositive zero or negative zero, then the result is the same as theargument.
- If the argument value is less than zerobut greater than -1.0, then the result is negativezero.
double java.lang.Math.rint(doublea)
Returns the double
value thatis closest in value to the argument and is equal to a mathematicalinteger. If two double
values that are mathematicalintegers are equally close, the result is the integer value that iseven. Special cases:
- If the argument value is already equal toa mathematical integer, then the result is the same as theargument.
- If the argument is NaN or an infinity orpositive zero or negative zero, then the result is the same as theargument.
long java.lang.Math.round(doublea)
Returns the closest long
tothe argument. The result is rounded to an integer by adding 1/2,taking the floor of the result, and casting the result to typelong
. In other words, the result is equal to the valueof the expression:
(long)Math.floor(a + 0.5d)
Special cases:
- If the argument is NaN, the result is 0.
- If the argument is negative infinity or any value less than or equal to the value of
Long.MIN_VALUE
, the result is equal to the value ofLong.MIN_VALUE
. - If the argument is positive infinity or any value greater than or equal to the value of
Long.MAX_VALUE
, the result is equal to the value ofLong.MAX_VALUE
.
Java语言中小数的取整