# Java Math round() Function: Complete Guide

Java math round() function is used to calculate the nearest int(or long) value of a float(or double) variable. The round() method comes in handy across various programs wherever such a conversion is needed. The rounded-off value is calculated by adding 0.5 to the passed value and then taking the floor of the result.

## Java Math round()

Java.lang.Math.round() is a built-in method used to round the decimal numbers to the nearest value. This method is used to return the closest long to the argument, with ties rounding to positive infinity.

### Syntax

```public static int round(float x)
public static long round(double x)
```

### Parameter(s)

The variable of float or double type whose value is to be rounded off to the closest int or long, respectively.

### Return Value

The rounded-off value by adding 0.5 to the passed value and then taking the floor, typecasting it to int or long depending on whether the passed variable is of float or double type. See the following figure.

#### Note

1. If the argument is NaN, then this method returns 0.
2. If the argument is negative infinity, then this method returns Long.MIN_VALUE.
3. If the argument is positive infinity, then this method returns Long.MAX_VALUE.

Consider the following examples.

#### Example1.java: The following example demonstrates how the return values are typecasted by displaying the wrapper classes of the returned variables.

```public class Example1 {
public static void main(String[] args) {
float a = 4.1f;
double b = 4.5;

System.out.println(((Object) (Math.round(a))).getClass().getName());
System.out.println(((Object) (Math.round(b))).getClass().getName());
}
}
```

#### Output

```->javac Example1.java
->java Example1
java.lang.Integer
java.lang.Long
```

#### Example2.java: The following example demonstrates how the passed values are rounded off.

```public class Example2 {
public static void main(String[] args) {

System.out.println("4.6 is rounded off to: " + Math.round(4.6f));
System.out.println("4.1 is rounded off to: " + Math.round(4.1f));
System.out.println("3.9 is rounded off to: " + Math.round(3.9f));
System.out.println("4.50 is rounded off to: " + Math.round(4.50));
System.out.println("4.51 is rounded off to: " + Math.round(4.51));
System.out.println("4.49 is rounded off to: " + Math.round(4.49));
}
}
```

#### Output

```->javac Example2.java
->java Example2
4.6 is rounded off to: 5
4.1 is rounded off to: 4
3.9 is rounded off to: 4
4.50 is rounded off to: 5
4.51 is rounded off to: 5
4.49 is rounded off to: 4```

#### Example3.java: The following example demonstrates the situation of passing a NaN value.

```public class Example3 {
public static void main(String[] args) {
System.out.println(Math.round(2.0 % 0));
}
}
```

#### Output

```->javac Example3.java
->java Example3
0
```

#### Example4.java: The following example demonstrates the situation of passing negative infinity or positive infinity.

```public class Example4 {
public static void main(String[] args) {

System.out.println(Math.round(-2.0 / 0));
System.out.println(Math.round(2.0 / 0));
}
}
```

#### Output

```->javac Example4.java
->java Example4
-9223372036854775808
9223372036854775807
```