Java StrictMath rint()方法
rint() 是Java中StrictMath类的内置方法,用于获取与参数值最接近且等于整数的双倍值。当作为整数的两个双倍值与给定参数的值同样接近时,它返回的整数值是偶数。当参数值已经等于整数时,它返回与参数相同的值;当参数为NaN或无穷大或正零或负零时,它返回与参数相同的值。
语法
public static double rint(double num)
参数: 该方法接受单参数 num ,为双数类型,使用该方法进行转换。
返回值: 该方法返回与整数最接近的浮点值。
示例 :
输入: num =72.2
输出:72.0
以下程序说明了rint()方法:
程序1 :
// 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);
}
}
输出
The Integer value closest to 87.1 = 87.0
The Integer value closest to 87.1 = 87.0
程序2
// 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