Java中的 StrictMath rint() 方法
rint()是Java中 StrictMath 类的内置方法,用于获取与参数值最接近且等于整数的 double 值。它返回整数值,即使作为整数的两个双精度值同样接近给定参数的值。当参数值已经等于整数时,它返回与参数相同的值,当参数是 NaN 或无穷大或正零或负零时,它返回与参数相同的值。
句法:
public static double rint(double num)
参数:该方法接受要使用该方法转换的double类型的单个参数num 。
返回值:该方法返回等于整数的最接近的浮点值。
例子 :
Input: num =72.2
Output: 72.0
下面的程序说明了 rint() 方法:
方案一:
java
// Java program 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);
}
}
java
// Java program 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 87.1 = 87.0
The Integer value closest to 87.1 = 87.0
方案二:
Java
// Java program 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