Mètode Java Math round() amb Exemple


El java.lang.Math.round() és una funció matemàtica integrada que retorna el llarg més proper a l'argument. El resultat s'arrodoneix a un nombre enter sumant 1/2 , agafant la paraula del resultat després d'afegir 1/2 i llançar el resultat a escriure llarg.

  • Si l'argument és No, el resultat és 0.
  • Si l'argument és infinit negatiu o qualsevol valor inferior o igual al valor de Enter.MIN_VALUE , el resultat és igual al valor de Integer.MIN_VALUE.
  • Si l'argument és infinit positiu o qualsevol valor superior o igual al valor de Enter.MAX_VALUE , el resultat és igual al valor de Integer.MAX_VALUE.

Sintaxi:

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

Devolucions:
El mètode retorna el valor de l'argument arrodonit al valor int més proper.

Exemple: Per mostrar el funcionament de la funció 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));> > > }> }>

Sortida:

4568 -3421 9223372036854775807