Java StrictMath ceil()方法及示例

来源:这里教程网 时间:2026-02-17 21:12:58 作者:

Java StrictMath ceil()方法及示例

java.lang.StrictMath类的所有方法:Set 1 , Set 2
java.lang.StrictMath.ceil() 是Java中的一个内置方法,用于返回最小的双倍值,大于或等于给定的双倍参数并等于一个整数。它产生了三个特殊的结果。

当给定参数等于整数时,结果与参数相同。当给定参数为NaN、无穷大、正0或负0时,结果与参数相同。当给定参数小于0且大于-1.0时,结果为负0。

语法

public static double ceil( _double num_ )

参数: 该方法接受一个双数类型的参数 num ,其上限值将被返回。
返回值: 该方法返回最接近负无穷大的最小浮点值,该值大于或等于给定的参数,也等于整数。
示例 :

Input: num = 2.7Output: 3.0Input: num = -8.7Output: -8.0

下面的程序说明了java.lang.StrictMath.ceil()方法:
程序1

// 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.0The Ceil value of 7.1 = 8.0The Ceil value of 3.5 = 4.0

程序2

// 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.0The Ceil value of -7.1 = -7.0The Ceil value of -3.5 = -3.0

相关推荐