Java中的 StrictMath ceil() 方法及示例
Java.lang.StrictMath 类的所有方法:Set 1, Set 2
Java.lang.StrictMath.ceil()是Java中的一个内置方法,用于返回大于或等于给定双精度参数且等于整数的最小双精度值。它产生了三个特殊的结果:
- 当给定参数等于整数时,结果与参数相同。
- 结果与给定参数为 NaN、无穷大、正零或负零时的参数相同。
- 当给定参数小于 0 且大于 -1.0 时,结果为负 0。
句法:
public static double ceil(double num)
参数:该方法接受一个要返回最大值的double类型的参数num 。
返回值:该方法返回最接近负无穷且大于或等于给定参数且也等于整数的最小浮点值。
例子 :
Input: num = 2.7
Output: 3.0
Input: num = -8.7
Output: -8.0
下面的程序说明了Java.lang.StrictMath.ceil() 方法:
方案一:
java
// Java program to illustrate the
// java.lang.StrictMath.ceil()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
double num1 = 8.7, num2 = 7.1, num3 = 3.5;
// It returns ceiling value
double cValue = StrictMath.ceil(num1);
System.out.println("The Ceil value of "+
num1+" = " + cValue);
cValue = StrictMath.ceil(num2);
System.out.println("The Ceil value of "+
num2+" = " + cValue);
cValue = StrictMath.ceil(num3);
System.out.println("The Ceil value of "+
num3+" = " + cValue);
}
}
java
// Java program to illustrate the
// java.lang.StrictMath.ceil()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
double num1 = -8.7, num2 = -7.1, num3 = -3.5;
// It returns ceiling value
double cValue = StrictMath.ceil(num1);
System.out.println("The Ceil value of "+
num1+" = " + cValue);
cValue = StrictMath.ceil(num2);
System.out.println("The Ceil value of "+
num2+" = " + cValue);
cValue = StrictMath.ceil(num3);
System.out.println("The Ceil value of "+
num3+" = " + cValue);
}
}
输出:
The Ceil value of 8.7 = 9.0
The Ceil value of 7.1 = 8.0
The Ceil value of 3.5 = 4.0
方案二:
Java
// Java program to illustrate the
// java.lang.StrictMath.ceil()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
double num1 = -8.7, num2 = -7.1, num3 = -3.5;
// It returns ceiling value
double cValue = StrictMath.ceil(num1);
System.out.println("The Ceil value of "+
num1+" = " + cValue);
cValue = StrictMath.ceil(num2);
System.out.println("The Ceil value of "+
num2+" = " + cValue);
cValue = StrictMath.ceil(num3);
System.out.println("The Ceil value of "+
num3+" = " + cValue);
}
}
输出:
The Ceil value of -8.7 = -8.0
The Ceil value of -7.1 = -7.0
The Ceil value of -3.5 = -3.0