logo

Metoda Java Math round() s primerom


The java.lang.Math.round() je vgrajena matematična funkcija, ki vrne argumentu najbližjo dolgo vrednost. Rezultat se s seštevanjem zaokroži na celo število 1/2 , vzeti besedo rezultata po dodajanju 1/2 in preoblikovati rezultat v vrsto long.

  • Če je argument ne, rezultat je 0.
  • Če je argument negativna neskončnost ali katera koli vrednost, manjša ali enaka vrednosti Celo število.MIN_VALUE , je rezultat enak vrednosti Integer.MIN_VALUE.
  • Če je argument pozitivna neskončnost ali katera koli vrednost, večja ali enaka vrednosti Celo število.MAX_VALUE , je rezultat enak vrednosti Integer.MAX_VALUE.

Sintaksa:



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

Vrne:
Metoda vrne vrednost argumenta, zaokroženo na najbližjo int vrednost.

primer: Za prikaz delovanja funkcije 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));> > >}> }>

>

>

Izhod:

4568 -3421 9223372036854775807>