java.lang.StrictMath.abs(float a)方法實例
java.lang.StrictMath.abs(float a) 方法返回一個浮點值的絕對值。如果參數不是負數,則返回該參數。如果參數爲負數,則返回該參數的負數(負負得正)。它包含了一些情況:
- 如果參數爲正零或負0,那麼結果爲正零。
- 如果參數爲無窮大,那麼結果爲正無窮大。
- 如果參數爲NaN,那麼結果爲NaN。
聲明
以下是**java.lang.StrictMath.abs()**方法的聲明
public static float abs(float a)
參數
- a -- 這是要確定其絕對值的參數。
返回值
該方法返回一個浮點數值的絕對值。
異常
- NA
例子
下面的例子顯示java.lang.StrictMath.abs()方法的使用。
package com.yiibai; import java.lang.*; public class StrictMathDemo { public static void main(String[] args) { float f1 = 78 , f2 = -9; // returns the absolute value of positive float value float fAbsValue = StrictMath.abs(f1); System.out.println("absolute value of " + f1 + " = " + fAbsValue); // returns the absolute value of negative float value fAbsValue = StrictMath.abs(f2); System.out.println("absolute value of " + f2 + " = " + fAbsValue); } }
讓我們來編譯和運行上面的程序,這將產生以下結果:
absolute value of 78.0 = 78.0 absolute value of -9.0 = 9.0