Java - StrictMath abs(float) 方法



描述

Java StrictMath abs(float a) 方法返回浮點值的絕對值。如果引數是非負數,則返回該引數。如果引數為負數,則返回該引數的負值。特殊情況 -

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

宣告

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

public static float abs(float a)

引數

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

返回值

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

異常

獲取正浮點值的絕對值示例

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

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

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

輸出

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

StrictMath.abs(4876.1875)=4876.1874

獲取零浮點值的絕對值示例

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

package com.tutorialspoint;

public class StrictMathDemo {

   public static void main(String[] args) {

      // get a float to find its absolute values
      float x = -0.0f;
   
      // 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 float to find its absolute values
      float x = -9999.555f;
   
      // 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.