## Java Math round()

round() returns closest long to the argument passed.

Following is the syntax of round() method.

`double rounded = round(double x)`

Since the definition of round() function has double datatype as argument, you can also pass floating point value as argument; because float could implicitly promote to double.

We shall learn about some of the special cases for round() method with examples.

### Example 1 – Math.round(double)

In the following example, we use round() method to round the given double number.

Java Program

```public class MathExample {
public static void main(String[] args) {
double a = 20.4;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`20`

If the input double value has a decimal part of exactly 0.5, then round() method rounds the argument towards the positive infinity.

Java Program

```public class MathExample {
public static void main(String[] args) {
double a = 20.5;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`21`

### Example 2 – Math.round(float)

In the following example, we pass an float value as argument to round() method.

Java Program

```public class MathExample {
public static void main(String[] args) {
float a = 2.1255F;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`2`

### Example 3 – Math.round(NaN)

In the following example, we pass Double.NaN as argument to round() method. As per the definition of the round() in Math class, the method should return zero.

Java Program

```public class MathExample {
public static void main(String[] args) {
double a = Double.NaN;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`0`

### Example 4 – Math.round() – With Positive Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to round() method. As per the definition of the round() method in Math class, the method should return Long.MAX_VALUE.

Java Program

```public class MathExample {
public static void main(String[] args) {
double a = Double.POSITIVE_INFINITY;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`9223372036854775807`

### Example 5 – Math.round() – With Negative Infinity as Argument

In the following example, we pass Double.POSITIVE_INFINITY as argument to round() method. As per the definition of the round() method in Math class, the method should return Long.MIN_VALUE.

Java Program

```public class MathExample {
public static void main(String[] args) {
double a = Double.NEGATIVE_INFINITY;
long rounded = Math.round(a);
System.out.println(rounded);
}
}```

Output

`-9223372036854775808`

### Conclusion

In this Java Tutorial, we learned about Java Math.round() function, with example programs.