Java - StrictMath abs(double) 方法



描述

Java StrictMath abs(double a) 方法返回雙精度數值的絕對值。如果引數非負,則返回該引數。如果引數為負,則返回該引數的相反數。特殊情況 -

  • 如果引數為正零或負零,則結果為正零。
  • 如果引數為無限大,則結果為正無窮大。
  • 如果引數為 NaN,則結果為 NaN。

宣告

以下是 java.lang.StrictMath.abs() 方法的宣告

public static double abs(double a)

引數

a - 需要確定絕對值的引數

返回值

此方法返回引數的絕對值。

異常

獲取正雙精度數值的絕對值示例

以下示例演示瞭如何使用 StrictMath abs() 方法獲取正雙精度數值的絕對值。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get a double to find its absolute values
      double x = 4876.1874d;
   
      // get and print its absolute value
      System.out.println("StrictMath.abs(" + x + ")=" + StrictMath.abs(x));
   }
}

輸出

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

StrictMath.abs(4876.1874)=4876.1874

獲取零雙精度數值的絕對值示例

以下示例演示瞭如何使用 StrictMath abs() 方法獲取零雙精度數值的絕對值。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get a double to find its absolute values
      double x = -0.0d;
   
      // get and print its absolute value
      System.out.println("StrictMath.abs(" + x + ")=" + StrictMath.abs(x));
   }
}

輸出

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

StrictMath.abs(-0.0)=0.0

獲取負雙精度數值的絕對值示例

以下示例演示瞭如何使用 StrictMath abs() 方法獲取負雙精度數值的絕對值。

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get a double to find its absolute values
      double x = -9999.555d;
   
      // get and print its absolute value
      System.out.println("StrictMath.abs(" + x + ")=" + StrictMath.abs(x));
   }
}

輸出

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

StrictMath.abs(-9999.555)=9999.555
java_lang_strictmath.htm
廣告

© . All rights reserved.