StrictMath 類在 Java 中有什麼用?


java.lang.StrictMath 是一個 final 類,是 Object 類的子類。StrictMath 類包含用於執行基本數字運算的方法,如基本指數、對數、平方根和三角函式。

我們不需要為 StrictMath 類建立例項,因為 StrictMath 類中的所有方法都是 static 方法。StrictMath 類的重要方法有 abs()、acos()、asin()、atan()、ceil()、floor()、log()、max()、min()、pow()、random()、round() 等。

語法

public final class StrictMath extends Object

示例

public class StrictMathTest {
   public static void main(String args[]) {
      System.out.println("Absolute Value: " + StrictMath.abs(-100.50));
      System.out.println("Ceil Value : " + StrictMath.ceil(100.55));
      System.out.println("Floor Value : " + StrictMath.floor(100.55));
      System.out.println("Maximum Value : " + StrictMath.max(100,200));
      System.out.println("Minimum Value : " + StrictMath.min(100,200));
      System.out.println("Random Value : " + StrictMath.random());
      System.out.println("Round Value: " + StrictMath.round(100.75));
      System.out.println("Square Root Value : " + StrictMath.sqrt(2));
      System.out.println("PI Value: " + StrictMath.PI);
      System.out.println("Log Value: " + StrictMath.log(10.55));
   }
}

輸出

Absolute Value: 100.5
Ceil Value : 101.0
Floor Value : 100.0
Maximum Value : 200
Minimum Value : 100
Random Value : 0.24051171771710933
Round Value: 101
Square Root Value : 1.4142135623730951
PI Value: 3.141592653589793
Log Value: 2.3561258599220753

更新於:2023 年 11 月 22 日

96 檢視

開啟您的 事業

完成課程獲得認證

開始
廣告
© . All rights reserved.