In this tutorial we are going to look at how to **round decimal numbers in java**. **Java** provides the types: float, double and BigDecimal for representing decimal numbers. The types float and double which are primitive types also have the wrapper classes Float and Double respectively.

1 2 | ```
float PI = 3.1415f;
double PI = 3.1415;
``` |

However, the types float and double cannot represent all real number precisely, and it is in this representation that results in precision issues. In this case if we need to deal with precise values such as performing financial calculations or working with currencies we should use the BigDecimal class.

If we want to print out a decimal number with n digits after decimal point, we can simply format the output String:

1 | ```
System.out.printf("2 digits after decimal point %.2f %n", PI);
``` |

Alternatively, we can format a decimal in java using the DecimalFormat Class

1 2 | ```
DecimalFormat df = new DecimalFormat("###.##");
System.out.println(df.format(PI));
``` |

Moreover DecimalFormat allows us to explicitly set a number of options that can control rounding behavior. For example we can set the rounding mode.

1 | ```
df.setRoundingMode(RoundingMode.HALF_DOWN);
``` |

To round doubles to n decimal places, we can use the BigDecimal class. We could also encapsulate this logic into a utility function that we can call.

1 2 3 4 5 6 7 8 | ```
public static double roundDouble(double value, int decPlaces) {
if (decPlaces < 0) {
throw new IllegalArgumentException();
}
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(decPlaces, RoundingMode.HALF_UP);
return bd.doubleValue();
}
``` |

hen working with BigDecimal we should always use the String constructor this prevents issues when working with inexact values.

We can also round decimals in java using the Apache Commons Math Library. We can easily added it to our project as a maven dependency as follows:

1 2 3 4 5 | ```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.5</version>
</dependency>
``` |

We can then use the Precision.round() method, which takes two arguments – value and scale:

1 | ```
Precision.round(PI, 2);
``` |

By default, this api is using the same HALF_UP rounding method but it can be changed by passing the rounding method as the third parameter.

We can also use the DoubleRounder from the decimal4j library. This library provides an efficient method for rounding doubles from 0 to 18 decimal places. Again we can add this library to our project using a maven dependency.

1 2 3 4 5 | ```
<dependency>
<groupId>org.decimal4j</groupId>
<artifactId>decimal4j</artifactId>
<version>1.0.3</version>
</dependency>
``` |

We can then round a decimal using the call

1 | ```
DoubleRounder.round(PI, 2);
``` |

Another approach of rounding numbers in java is to use Math.Round() Method. we can decide on the number of decimal places by multiplying and dividing by 10^n:

1 2 3 4 | ```
public static double round(double value, int decPlaces) {
double scale = Math.pow(10, decPlaces);
return Math.round(value * scale) / scale;
}
``` |

However, this approach is not recommend due to truncating the value which can result in values rounded incorrectly.

In this short tutorial, we looked at different approaches to rounding decimal numbers in java to n decimal places. We can output a formatted decimal value without changing its value. We also looked at using the BigDecimal class to round a decimal in java and looked at a number of libraries we could use to perform this task.