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 connect to ssh server using JSch? - January 5, 2022
- How do I mail merge Word document in Java? - June 17, 2021
- How do I format EditText for currency input in Android? - May 21, 2021