Groovy - round()



round 方法返回最接近的 long 或 int,如方法返回型別所示。

語法

long round(double d)
  
int round(float f) 

引數

  • d - double 或 float 基本資料型別
  • f - float 基本資料型別

返回值

此方法返回最接近的 longint(如方法的返回型別所示)作為引數。

示例

以下演示了此方法的使用方法 −

class Example { 
   static void main(String[] args) { 
      double d = 100.675; 
      double e = 100.500; 
		
      float f = 100; 
      float g = 90f;  
		
      System.out.println(Math.round(d)); 
      System.out.println(Math.round(e)); 
      System.out.println(Math.round(f));
      System.out.println(Math.round(g)); 
   } 
}

執行以上程式後,將獲得以下結果 −

101 
101 
100 
90
groovy_numbers.htm
廣告
© . All rights reserved.