Java abs() 方法
abs() 返回參數的絕對值。參數可以是 int, float, long, double, short, byte類型。
語法
各個類型的方法格式類似如下:
double abs(double d) float abs(float f) int abs(int i) long abs(long lng)
參數
- 任何原生數據類型。
返回值
返回參數的絕對值。
實例
public class Test{
public static void main(String args[]){
Integer a = -8;
double d = -100;
float f = -90;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}
}
編譯以上程式,輸出結果為:
8 100.0 90.0

Java Number類