The java.lang.Math.round() is a built-in math function which returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result after adding 1/2, and casting the result to type long.
- If the argument is NaN, the result is 0.
- If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.
Syntax:
public static int round(float val)
Parameter:
val - floating-point value to be rounded to an integer.
Returns:
The method returns the value of the argument rounded to the nearest int value.
Example: To show working of java.lang.Math.round() function
import java.lang.Math;
class Gfg {
public static void main(String args[])
{
float x = 4567 .9874f;
System.out.println(Math.round(x));
float y = - 3421 .134f;
System.out.println(Math.round(y));
double positiveInfinity = Double.POSITIVE_INFINITY;
System.out.println(Math.round(positiveInfinity));
}
}
|
Output:
4568
-3421
9223372036854775807
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Apr, 2018
Like Article
Save Article