rint()是Java中StrictMath类的内置方法,用于获取值最接近参数且等于整数的double值。它返回一个整数值,当作为整数的两个double值均相等地接近给定参数的值时,它甚至是整数。当参数值已经等于整数时,它返回与参数相同的值;当参数为NaN或无穷大或正零或负零时,它返回与参数相同的值。
用法:
public static double rint(double num)
参数:该方法接受要使用此方法转换的双精度类型的单个参数num。
返回值:该方法返回等于整数的最接近的浮点值。
例子:
Input: num =72.2
Output: 72.0
以下示例程序旨在说明rint()方法:
示例1:
// Java praogram to illustrate the
// java.lang.StrictMath.rint()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// Get a double number
double num1 = 87.1;
// Convert the double number using rint() method
double rint_Value = StrictMath.rint(num1);
// Print the result
System.out.println(" The Integer value closest to "
+ num1 + " = " + rint_Value);
// Get a double number
double num2 = 65.9;
// Convert the double number using rint() method
rint_Value = StrictMath.rint(num1);
// Print the result
System.out.println(" The Integer value closest to "
+ num1 + " = " + rint_Value);
}
}
输出:
The Integer value closest to 87.1 = 87.0
The Integer value closest to 87.1 = 87.0
示例2:
// Java praogram to illustrate the
// java.lang.StrictMath.rint()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// Get a double number
double num1 = -65.5;
// Convert the double number using rint() method
double rint_Value = StrictMath.rint(num1);
// Print the result
System.out.println(" The Integer value closest to "
+ num1 + " = " + rint_Value);
// Get a double number
double num2 = -42.7;
// Convert the double number using rint() method
rint_Value = StrictMath.rint(num1);
// Print the result
System.out.println(" The Integer value closest to "
+ num1 + " = " + rint_Value);
}
}
输出:
The Integer value closest to -65.5 = -66.0
The Integer value closest to -65.5 = -66.0