CodeGym /Java 博客 /随机的 /Java Math.ceil() 方法
John Squirrels
第 41 级
San Francisco

Java Math.ceil() 方法

已在 随机的 群组中发布
在我们转向 Java 中的 ceil 方法之前,最好熟悉一下数学中的 ceil 函数。

数学中的ceil函数是什么?

“ceil 函数将十进制数转换为直接最大整数。”
如果传递的数字已经是整数或整数,则相同的数字是上限值。但是,如果您将空值传递给数学中的 ceil 函数,您将得到一个“零”。

Java 中的 Math.ceil() 方法是什么?

Java 提供了一种内置的方法来计算数学中的 ceil 函数。我们可以通过将“double”类型的参数传递给方法Math.ceil()来自由使用。在转到用例之前,让我们先看看一些边界情况。
  • 如果参数“ double ”也是一个数学“整数”[例如:2.0 与 2 相同] -结果等于整数[即;2本身]
  • 如果参数 (let parameter = x)小于 0 但大于 -1 [ -1 > x < 0 ] -结果等于负零 [-0]
  • 如果参数为NaN、+0、-0 或 ∞ -结果与参数相同
  • 如果参数为“ null ”——与您得到零的数学 ceil 函数不同,这里您将得到java.lang.NullPointerException

例子


class Main {

  public static void main(String[] args) {
  
    Double totalStudentsInClass = 25.0;
    Double flourWeightInKgs = 5.13;
    Double aPoundOfOxygenInLitres = 0.3977; 
    Double startingArrayIndexInJava = 0.0;
    Double aSelfDrivingCar = Double.NaN;
    Double numberOfStarsInTheSky = Double.POSITIVE_INFINITY;
    // For parameter  [ -1 > x < 0 ]
    Double x = -0.025;
    
    
    // using Math.ceil() method
    System.out.println("Total Students In Class = " + Math.ceil(totalStudentsInClass)); 
    System.out.println("Flour Weight In Kgs = " + Math.ceil(flourWeightInKgs));
    System.out.println("A Pound of Oxygen in Litres = " + Math.ceil(aPoundOfOxygenInLitres));
    System.out.println("Starting Array Index In Java = " + Math.ceil(startingArrayIndexInJava));
    System.out.println("A Self Driving Car = " + Math.ceil(aSelfDrivingCar));
    System.out.println("Number Of Stars In The Sky = " + Math.ceil(numberOfStarsInTheSky));
    System.out.println("Positive Zero = " + Math.ceil(+0.0));
    System.out.println("Negative Zero = " + Math.ceil(-0.0));
    System.out.println("x = " + x + " [ -1 > x < 0 ] = " + Math.ceil(-0.0));
  }

}

输出

班级学生总数 = 25.0 面粉重量(公斤)= 6.0 一磅氧气(升)= 1.0 Java 中的起始数组索引 = 0.0 自动驾驶汽车 = NaN 天空中的星星数量 = 无穷大 正零 = 0.0 负零 = -0.0 x = -0.025 [ -1 > x < 0 ] = -0.0

结论

为了更好地掌握 java ceil 方法,我们建议您还具备数学上限函数的一些良好背景知识。拥有坚实的基础有助于建立更坚实的基础。尽管如此,您始终可以继续学习、成长,并且一如既往地不要忘记练习!
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION