Groovy-round

提供:Dev Guides
移動先:案内検索

グルーヴィー-round()

メソッドroundは、メソッドの戻り型で指定された最も近いlongまたはintを返します。

構文

long round(double d)

int round(float f)

パラメーター

  • d-doubleまたはfloatプリミティブデータ型
  • f-floatプリミティブデータ型

戻り値

このメソッドは、メソッドの戻り値の型で示されるように、引数に最も近い long または int を返します。

以下は、このメソッドの使用例です-

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