Java round()方法

round()方法返回最接近参数的longint类型值,由方法返回类型给出。

语法

此方法有以下变体 -

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

参数

  • d - doublefloat原始数据类型。
  • f - 浮点原始数据类型。

返回值

  • 此方法返回最接近的longint类型值,使用方法指定返回类型来返回参数。

示例

public class Test { 

   public 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

上一篇: Java Number类 下一篇: Java快速入门