帶示例的 Java floor() 方法


java.lang.Math.floor() 返回小於或等於引數且等於數學整數的最大(最接近正無窮大)的 double 值。特殊情況 −

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

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

我們現在看一個示例來實現 Java 中的 floor() 方法 −

示例

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      // get two double numbers
      double x = 60984.1;
      double y = -497.99;
      // call floor and print the result
      System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
      System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
      System.out.println("Math.floor(0)=" + Math.floor(0));
   }
}

Math.floor(60984.1)=60984.0
Math.floor(-497.99)=-498.0
Math.floor(0)=0.0

示例

我們現在看另一個示例,其中我們將檢查負值和其他值 −

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      // get two double numbers
      double x = 0.0;
      double y = -5.7;
      double z = 1.0/0;
      // call floor and print the result
      System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
      System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
      System.out.println("Math.floor(" + z + ")=" + Math.floor(z));
   }
}

Math.floor(0.0)=0.0
Math.floor(-5.7)=-6.0
Math.floor(Infinity)=Infinity

更新於: 24-9 月-2019

187 次瀏覽

啟動你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.