Java - Math floor(double) 方法



描述

Java Math floor(double a) 方法返回小於或等於引數且等於數學整數的最大(最接近正無窮大)雙精度浮點數。特殊情況

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

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

宣告

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

public static double floor(double a)

引數

a − 一個值。

返回值

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

異常

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

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

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 10.7;

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

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

Math.floor(10.7)=10.0

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

以下示例演示了 Math floor() 方法對零值的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get a double number
      double x = 0.0;

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

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

Math.floor(0.0)=0.0

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

以下示例演示了 Math floor() 方法對負數的使用。

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

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

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

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

Math.floor(-10.7)=-11.0
java_lang_math.htm
廣告