Java Math CEIL示例
时间:2020-02-23 14:35:04 来源:igfitidea点击:
Java Math CEIL函数用于获得大于数字的最小整数。
现实例子
让我们说一家电话每分钟向我们收费1美元。
当我们与某人交谈1分5秒钟时,它仍将为电话收取2美元。
所以电话可以使用Math.Ceil函数,从1分5秒到2分钟。
语法
public static double ceil(double x)
例子
让我们在该示例中使用Math的CEIL函数。
package org.igi.theitroad;
public class MathCeilMain {
public static void main(String[] args) {
//Use math.ceil to get smaller integer greater
//than number
System.out.println("Ceil value for 2.6 = " + Math.ceil(2.6));
System.out.println("Ceil value for 26.1 = " + Math.ceil(26.1));
System.out.println("Ceil value for -0.8 = " + Math.ceil(-0.8));
System.out.println("Ceil value for -6.2 = " + Math.ceil(-6.2));
float f=4.6f;
System.out.println("Ceil value for 4.6f = " + Math.ceil(f));
}
}
运行上述程序时,程序的输出将是:
Ceil value for 2.6 = 3.0 Ceil value for 26.1 = 27.0 Ceil value for -0.8 = -0.0 Ceil value for -6.2 = -6.0 Ceil value for 4.6f = 5.0
几点有关Math.CEIL函数:
- 如果是nan或者无穷大或者零,它将为我们提供与争论相同的结果。
- 如果传递已等于整数的值,则会导致相同的值。

