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.