The java.lang.Math.round() to wbudowana funkcja matematyczna, która zwraca długość najbliższą argumentowi. Wynik zaokrągla się do liczby całkowitej poprzez dodanie 1/2 , biorąc pod uwagę wynik po dodaniu 1/2 i rzutując wynik na typ długi.
- Jeśli argumentem jest NIE, wynik wynosi 0.
- Jeśli argumentem jest ujemna nieskończoność lub dowolna wartość mniejsza lub równa wartości Liczba całkowita.MIN_VALUE , wynik jest równy wartości Integer.MIN_VALUE.
- Jeśli argumentem jest dodatnia nieskończoność lub dowolna wartość większa lub równa wartości Liczba całkowita.MAX_VALUE , wynik jest równy wartości Integer.MAX_VALUE.
Składnia:
public static int round(float val) Parameter: val - floating-point value to be rounded to an integer.>
Zwroty:
Metoda zwraca wartość argumentu zaokrągloną do najbliższej wartości typu int.
Przykład: Aby pokazać działanie funkcji 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));> > > }> }> |
>
>
Wyjście:
4568 -3421 9223372036854775807>