Groovy - abs()



此方法返回引數的絕對值。引數可以是 int、float、long、double、short、byte。

語法

double abs(double d) 
float abs(float f) 
int abs(int i) 
long abs(long lng)

引數 - 任意基元資料型別。

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

示例

以下是使用此方法的示例。

class Example { 
   static void main(String[] args) { 
      Integer a = -8; 
      double b = -100; 
      float c = -90; 
		
      System.out.println(Math.abs(a)); 
      System.out.println(Math.abs(b)); 
      System.out.println(Math.abs(c)); 
   } 
}

當我們執行以上程式時,會得到以下結果 -

8 
100.0 
90.0
groovy_numbers.htm
廣告
© . All rights reserved.