CodeGym /Java Blog /Java Math /Java Math round() method
Author
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

# Java Math round() method

All programming languages and computers in general can't work with infinite numbers. Rounding and trimming of numbers follows us literally everywhere, because the nature of modern computers is like that. Java language has a special class for mathematical operations — Math, and it has a method that allows you to round numbers the way we need it. Here we’ve got the Math.round() method and in this article we will explain how to use it.

## Math.round() syntax

java.lang.Math.round() is a math method that returns the nearest long or integer to its argument. The result of Java Math round() is rounded to an integer by adding 1/2 and taking the floor of the result after adding 1/2. After this operation, the number is cast to a long or int type. The syntax of the round() method is:
``````
Math.round(value)
``````
round() just like the most of Math class methods is static. Value argument could be float or double. The method returns the nearest int (in case of float value) or long (in case of double value) number to the argument, with ties rounding to positive infinity.

## Special cases of Math.round()

• If the argument is NaN, the result will be 0.

• If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result will be 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.

## Java Math.round() example

Let’s have a program and demonstrate the Math round() method with examples of different arguments, float and double.
``````
public class MathExample {
//java.lang.Math.round() method example with float and double arguments
public static void main(String[] args) {
double e = 2.71828;
float pi = 3.1415f;
//Math.round() method: float turns to int
int intOfPi = Math.round(pi);
//Math.round() method: double  turns to long
long intOfE = Math.round(e);
System.out.println("integer part of pi = " + intOfPi);
System.out.println("integer part of e = " + intOfE);

}

}
``````
The output of this program is:
integer part of pi = 3 integer part of e = 3
As you can see, one of the numbers has been rounded up and the other rounded down to a smaller integer. In both cases, the result is the nearest integer. That’s how the Java.lang.Math.round() method works.