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:
- If the argument is NaN, the method will return zero.
- If the argument is negative infinity or any value less than or equal to the value of an Long.MIN_VALUE, the method will return an Long.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to Long.MAX_VALUE, the method will return Long.MAX_VALUE.
These special cases make sure that the Math.round() methods work correctly.
Syntax of round() Method
public static long round(float val)
- Parameter: This method takes a single parameter val of type float, which is a float that will be rounded to nearest integer.
- Return Type: This method returns the closest rounded value as long.
Now, we are going to discuss some examples for better understanding and clarity.
Examples of Java Math round() Method
Example 1: In this example, we will see the basic usage of Math.round() method.
Java
// Java program to demonstrate the working
// of Math.round() method
import java.lang.Math;
class Geeks {
// driver code
public static void main(String args[]) {
// float numbers
float x = 4567.9874f;
// find the closest long for these floats
System.out.println(Math.round(x));
}
}
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.
Java
// Java program to demonstrate the working
// of Math.round() method for special cases
import java.lang.Math;
class Geeks {
// driver code
public static void main(String args[]) {
// float numbers
float x = 4567.9874f;
// find the closest long for these floats
System.out.println(Math.round(x));
float y = -3421.134f;
// find the closest long for these floats
System.out.println(Math.round(y));
double p = Double.POSITIVE_INFINITY;
// returns the Long.MAX_VALUE value when
System.out.println(Math.round(p));
}
}
Output4568
-3421
9223372036854775807
Explanation: When the input is Double.POSITIVE_INFINITY, Math.round() returns Long.MAX_VALUE, which is 9223372036854775807.
Similar Reads
Java Math round() method with Example 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
2 min read
MathContext toString() Method in Java The MathContext.toString() method in Java is a part of java.math package. This method is used to define the precision and rounding behaviour for BigDecimal operations. This method returns the string representation of a MathContext object's context settings. The string returned represents the setting
2 min read
BigDecimal round() Method in Java The java.math.BigDecimal.round(MathContext m) is an inbuilt method in Java that returns a BigDecimal value rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place. Syntax: public BigDecimal round(MathContext m) Parameters: The method accepts a single
2 min read
Math fma() method in Java with Examples In Math class, there are two types of fma() method depending upon the parameters passed to it. The methods are: fma(double a, double b, double c): This method of Math class is used to return the exact product of the first two doubles with the addition of the third double and then round the result to
5 min read
java.math class and its methods | Set 1 Math class provides mathematical functions to perform basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. cosh, sin, tan, abs, bitLength, multiply and many more. Implementations of the functions of Math class do not return bit-for-bit same results. Henc
4 min read
Java Guava | mean() method of IntMath Class The mean(int x, int y) method of Guava's IntMath class accepts two parameters x and y and calculates arithmetic mean of them rounded towards negative infinity. This method is overflow resilient. Syntax : public static int mean(int x, int y) Parameters: This method accepts two parameters x and y whic
2 min read