Java - Math ceil(double) 方法



描述

Java Math ceil(double a) 返回大於或等於引數且等於數學整數的最小(最接近負無窮大)雙精度值。特殊情況:

  • 如果引數值已經等於數學整數,則結果與引數相同。

  • 如果引數是 NaN、無窮大、正零或負零,則結果與引數相同。

  • 如果引數值小於零但大於 -1.0,則結果為負零。

請注意,Math.ceil(x) 的值與 -Math.floor(-x) 的值完全相同。

宣告

以下是java.lang.Math.ceil() 方法的宣告

public static double ceil(double a)

引數

a − 一個值。

返回值

此方法返回大於或等於引數且等於數學整數的最小(最接近負無窮大)浮點值。

異常

獲取大於或等於正數的最小值示例

以下示例演示了 Math ceil() 方法的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 10.7;

      // print the ceil of the number
      System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.ceil(10.7)=11.0

獲取大於或等於零的最小值示例

以下示例演示了 Math ceil() 方法在零值上的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 0.0;

      // print the ceil of the number
      System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.ceil(0.0)=0.0

獲取大於或等於負數的最小值示例

以下示例演示了 Math ceil() 方法在負數上的用法。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = -10.7;

      // print the ceil of the number
      System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.ceil(-10.7)=-10.0
java_lang_math.htm
廣告