Java Mathのround()メソッドと例


java.lang.Math.round() は、引数に最も近いlongを返す組み込みの数学関数です。結果は加算により整数に丸められます。 1/2 、1/2 を加算した後の結果のフロアを取得し、結果を long 型にキャストします。

  • 引数が次の場合 いいえ、 結果は0です。
  • 引数が負の無限大、または次の値以下の値の場合 整数.MIN_VALUE の場合、結果は Integer.MIN_VALUE の値と等しくなります。
  • 引数が正の無限大、または次の値以上の値の場合 整数.MAX_VALUE の場合、結果は Integer.MAX_VALUE の値と等しくなります。

構文:

public static int round(float val) Parameter: val - floating-point value to be rounded to an integer. 

戻り値:
このメソッドは、最も近い int 値に丸められた引数の値を返します。

例: java.lang.Math.round() 関数の動作を示すには




// Java program to demonstrate working> // of java.lang.Math.round() method> import> java.lang.Math;> > class> Gfg {> > > // driver code> > public> static> void> main(String args[])> > {> > // float numbers> > float> x => 4567> .9874f;> > > // find the closest int for these floats> > System.out.println(Math.round(x));> > > float> y = -> 3421> .134f;> > > // find the closest int for these floats> > System.out.println(Math.round(y));> > > double> positiveInfinity = Double.POSITIVE_INFINITY;> > > // returns the Integer.MAX_VALUE value when> > System.out.println(Math.round(positiveInfinity));> > > }> }>

出力:

4568 -3421 9223372036854775807