![]() |
VOOZH | about |
The Math.round() method is a part of the java.lang.Math library. This method returns the closest long to the argument. In this article, we are going to discuss how this method works for regular values and for special cases.
Note: If the fraction is less than 0.5 then the number is rounded down and if the fraction is more than 0.5 then the number is rounded up.
Special Cases:
These special cases make sure that the Math.round() methods work correctly.
public static long round(float val)
Now, we are going to discuss some examples for better understanding and clarity.
Example 1: In this example, we will see the basic usage of Math.round() method.
4568
Explanation: Here, we rounded the value of x to the nearest long.
Example 2: In this example, we will see how the round() method handles all the special cases.
4568 -3421 9223372036854775807
Explanation: When the input is Double.POSITIVE_INFINITY,Math.round() returns Long.MAX_VALUE, which is 9223372036854775807.