使用 Java 中的 Math.ceil 獲取數字的上限值


為了在 Java 中獲取數字的上限值,使用 java.lang.Math.ceil() 方法。Math.ceil() 方法返回比引數大或等於引數且在數字線上具有與數學整數相等的值的最小(最接近負無窮大)double 值。如果引數為 NaN 或無窮大或正零或負零,則結果與引數相同。如果引數值小於 0 但大於 -1.0,則返回的值為負零。

宣告 − java.lang.Math.ceil() 方法宣告如下 −

public static double ceil(double a)

我們來看看一個程式,該程式將在 Java 中獲取數字的上限值。

示例

線上演示

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initialising some double values
      double a = -100.01d;
      double b = 34.6;
      double c = 600;
      // printing their ceiling values
      System.out.println("Ceiling value of " + a + " = " + Math.ceil(a));
      System.out.println("Ceiling value of " + b + " = " + Math.ceil(b));
      System.out.println("Ceiling value of " + c + " = " + Math.ceil(c));
   }
}

輸出

Ceiling value of -100.01 = -100.0
Ceiling value of 34.6 = 35.0
Ceiling value of 600.0 = 600.0

更新於:26-6-2020

2K+ 次瀏覽

開啟您的 職業生涯

完成課程後取得認證

開始
廣告