The example below show you some methods of the `Math`

class that can be used to round the value of a number. These methods are `Math.ceil()`

, `Math.floor()`

and `Math.round()`

.

```
package org.kodejava.math;
public class GetRoundedValueExample {
public static void main(String[] args) {
Double number = 1.5D;
// Get the smallest value that is greater than or equal to the
// argument and is equal to a mathematical integer
double roundUp = Math.ceil(number);
System.out.println("Result of rounding up of " + number + " = " + roundUp);
// Get the largest value that is less than or equal to the
// argument and is equal to a mathematical integer
double roundDown = Math.floor(number);
System.out.println("Result of rounding down of " + number + " = " + roundDown);
// Get the closest long value to the argument
long round1 = Math.round(number);
System.out.println("Rounding result of " + number + " (in long) = " + round1);
// Get the closest int value to the argument
int round2 = Math.round(number.floatValue());
System.out.println("Rounding result of " + number + " (in int) = " + round2);
}
}
```

Here are the result of the program:

```
Result of rounding up of 1.5 = 2.0
Result of rounding down of 1.5 = 1.0
Rounding result of 1.5 (in long) = 2
Rounding result of 1.5 (in int) = 2
```

Latest posts by Wayan (see all)

- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024