Java abs() 方法
abs() 返回參數(shù)的絕對(duì)值。參數(shù)可以是 int, float, long, double, short, byte類型。
語法
各個(gè)類型的方法格式類似如下:
double abs(double d) float abs(float f) int abs(int i) long abs(long lng)
參數(shù)
- 任何原生數(shù)據(jù)類型。
返回值
返回參數(shù)的絕對(duì)值。
實(shí)例
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)); } }
編譯以上程序,輸出結(jié)果為:
8 100.0 90.0
更多建議: