Java.lang.Math.rint() 方法
描述
java.lang.Math.rint(double a) 返回与参数值最接近且等于数学整数的双精度值。 如果两个作为数学整数的双精度值同样接近,则结果是偶数的整数值。 特别案例 −
如果参数值已经等于一个数学整数,则结果与参数相同。
如果参数是 NaN 或无穷大或正零或负零,则结果与参数相同。
声明
以下是 java.lang.Math.rint() 方法的声明。
public static double rint(double a)
参数
a − 一个 double 值。
返回值
此方法将最接近的浮点值返回到等于数学整数的 a。
异常
NA
示例
下面的例子展示了 lang.Math.rint() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 1654.9874;
double y = -9765.134;
// find the closest integers for these double numbers
System.out.println("Math.rint(" + x + ")=" + Math.rint(x));
System.out.println("Math.rint(" + y + ")=" + Math.rint(y));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Math.rint(1654.9874)=1655.0 Math.rint(-9765.134)=-9765.0

