How to limit decimal places in Java

Java

In this tutorial, we will understand how to limit decimal places in Java based on the requirement. There are different methods to round a number to n decimal places in Java.

For this, the input string is generally a float or a double value, which consists of several digits after the decimal point. There may be situations, where we want to limit the number of digits after the decimal.

Let us see each method in detail with an example below.

Using the format() method

In this example, we will see how to use the format() method, to limit the decimal places. The first argument specifies the format. `"%.2f"` denotes 2 decimal places, `"%.3f"` denotes 3 decimal places, and so on. Hence in the format argument, we can mention the limit of the decimal places. The second argument is the input value.

```public class LimitDecimalDemo {

public static void main(String[] args) {
Double d = 12345.783214;
System.out.println("Original value: " + d);
System.out.println("2 decimal format: ");
System.out.format("%.2f", d);
System.out.println("\n3 decimal format: ");
System.out.format("%.3f", d);
System.out.println("\n4 decimal format: ");
System.out.format("%.4f", d);
}

}
```
```Original value: 12345.783214
2 decimal format:
12345.78
3 decimal format:
12345.783
4 decimal format:
12345.7832```

Using String.format

There may also be situations when we want to append 0 after the decimal point for an n number of digits. Below is an example that illustrates the same. For this, we can use the `String.format()` method.

```public class LimitDecimalDemo {

public static void main(String[] args) {

Double n = 3.0;
System.out.println(String.format("%.5f", n));
System.out.println(String.format("%.3f", n));

}

}
```
```3.00000
3.000
```

Using DecimalFormat

The DecimalFormat class has various format options to round up the decimal places based on the initialized format. In the below example, we have initialized the `DecimalFormat` with the format as `"#.##"`. This means it limits the decimal place up to 2 digits.

ScheduledExecutorService in Java

We can also use the `setRoundingMode` method to round a number for n decimal places for the initialized format. For example, `RoundingMode.DOWN` and `RoundingMode.FLOOR` rounds the digit to the lower value. The remaining rounding modes round to the highest value.

```import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalFormatDemo {

public static void main(String[] args) {
Double d = 1234.675389;
DecimalFormat df = new DecimalFormat("#.##");

System.out.println("Original input value: " + d);
System.out.println("Using DecimalForamt: " + df.format(d));

df.setRoundingMode(RoundingMode.DOWN);
System.out.println("Using Rounding mode DOWN: " + df.format(d));

df.setRoundingMode(RoundingMode.CEILING);
System.out.println("Using Rounding mode CEILING: " + df.format(d));

df.setRoundingMode(RoundingMode.UP);
System.out.println("Using Rounding mode UP: " + df.format(d));

df.setRoundingMode(RoundingMode.FLOOR);
System.out.println("Using Rounding mode FLOOR: " + df.format(d));

df.setRoundingMode(RoundingMode.HALF_DOWN);
System.out.println("Using Rounding mode HALFDOWN: " + df.format(d));

df.setRoundingMode(RoundingMode.HALF_EVEN);
System.out.println("Using Rounding mode HALFEVEN: " + df.format(d));

df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println("Using Rounding mode HALFUP: " + df.format(d));
}

}
```
```Original input value: 1234.675389
Using DecimalForamt: 1234.68
Using Rounding mode DOWN: 1234.67
Using Rounding mode CEILING: 1234.68
Using Rounding mode UP: 1234.68
Using Rounding mode FLOOR: 1234.67
Using Rounding mode HALFDOWN: 1234.68
Using Rounding mode HALFEVEN: 1234.68
Using Rounding mode HALFUP: 1234.68
```

Using Math.round()

The `Math.round()` method is another method to limit the decimal places in Java. If we want to round a number to 1 decimal place, then we multiply and divide the input number by 10.0 in the `round()` method. Similarly, for 2 decimal places, we can use 100.0, for 3 decimal places, we can use 1000.0, and so on.

```public class LimitDecimalDemo {

public static void main(String[] args) {

Double n = 123.4567;
System.out.println("Original value: " + n);

Double d = (double) (Math.round(n*10.0)/10.0);
System.out.println(d);

d = (double) (Math.round(n*100.0)/100.0);
System.out.println(d);

d = (double) (Math.round(n*1000.0)/1000.0);
System.out.println(d);
}

}
```
```Original value: 123.4567
123.5
123.46
123.457
```

Using apache commons library

The apache-commons math3 library contains a Precision class method `round()` that we can use to round a number to the required number of decimal places. The first parameter is the input value and the second parameter is the number of decimal places to limit. If we pass 2, then it will consider 2 decimal places and so on.

How to compare two characters in Java

Below is an example that illustrates the use of the Precision class.

```import org.apache.commons.math3.util.Precision;

public class DecimalPrecision {

public static void main(String[] args) {
Double d = 456.98234;

Double n = Precision.round(d,2);
System.out.println("Original value: " + d);
System.out.println("Formatted value: " + n);

n = Precision.round(d, 3);
System.out.println("Formatted value: " + n);
}

}
```
```Original value: 456.98234
Formatted value: 456.98
Formatted value: 456.982
```

In this article we have covered limit decimal places in Java. You might be interested in reading How to Convert Int to String in Java

Reference