Metodo Java Math round() con l'esempio


IL java.lang.Math.round() è una funzione matematica incorporata che restituisce il valore long più vicino all'argomento. Il risultato viene arrotondato a un numero intero aggiungendo 1/2 , prendendo la parola del risultato dopo aver aggiunto 1/2 e trasformando il risultato in long.

  • Se l'argomento è NO, il risultato è 0.
  • Se l'argomento è infinito negativo o qualsiasi valore inferiore o uguale al valore di Intero.MIN_VALUE , il risultato è uguale al valore di Integer.MIN_VALUE.
  • Se l'argomento è infinito positivo o qualsiasi valore maggiore o uguale al valore di Intero.MAX_VALUE , il risultato è uguale al valore di Integer.MAX_VALUE.

Sintassi:

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

Ritorna:
Il metodo restituisce il valore dell'argomento arrotondato al valore int più vicino.

Esempio: Per mostrare il funzionamento della funzione 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));> > > }> }>

Produzione:

4568 -3421 9223372036854775807