# Rounding numbers in Java

Rounding numbers to 2 or 3 decimal places is a common requirement for a Java programmer. Fortunately, the Java API provides several ways to round numbers in Java. You can round any floating-point numbers in Java to n places using Math.round(), BigDecimal or DecimalFormat.

Personally, I prefer to use BigDecimal to round any number in Java because of its user-friendly API and support for multiple rounding modes. Also, if you work in the financial industry, it's best to do cash settlements in BigDecimal rather than double, as I said in my previous post about common Java mistakes.

One of the important things to keep in mind when rounding numbers is the rounding mode. Rounding mode decides how to round a discarded fraction and knowing the popular rounding modes.

If you're new to Java, I suggest you first start learning the basics like data types, floatvslong, and floatvsdouble before moving on to the tricky topic of rounding numbers in Java.

## 3 Ways to Round Floating Point Numbers in Java

Without wasting any more time, here are 3 different ways to round numbers in Java. They use the RoundingMode and DecimalFormat class in Java to format floating-point numbers and round them to two decimal places.

### Rounding modes in Java

An important thing to know before rounding numbers is the rounding mode. In general, the programmer says that if the least significant digit is less than five, then it will be rounded to a smaller figure, more than five to a larger figure, and they get confused when it is equidistant.

Rounding modes determine how the least significant digit should be rounded. The BigDecimal class has a rounding mode defined as an integer constant, which was later replaced by the RoundingMode enumeration from Java 1.5. Here are some of the important rounding modes to remember:

- RoundingMode.HALF_DOWN: Round down if both neighbors are equidistant as 2.5, round up to 2.0
- RoundingMode.HALF_UP: Round up if both neighbors are at the same distance, such as 2.5, rounded to 3.0
- RoundingMode.HALF_EVEN: Rounding towards an even neighbor if both neighbors are equidistant, e.g. 2.5 rounds to 2.0 and 5.5 rounds to 6.0

There are a few more rounding modes, such as UP, DOWN, FLOOR, and CEILING, which are also worth knowing. If you want to learn more about floating-point numbers and these concepts, I suggest you join the Basics of Java: Pluralsight Java Language Course. This is a great foundational course for Java developers.

**Rounding numbers in Java**

Number to enter | 5,5 | 2,5 | 1,6 | 1,1 | 1,0 | -1,0 | -1,1 | -1,6 | -2,5 | -5,5 |

Ceiling | 6 | 3 | 2 | 2 | 1 | -1 | -1 | -1 | -2 | -5 |

-5 | 5 | 2 | 1 | 1 | 1 | -1 | -1 | -1 | -2 | 5 |

Floor | 5 | 2 | 1 | 1 | 1 | -1 | -2 | -2 | -3 | -6 |

Half_Down | 5 | 2 | 2 | 1 | 1 | -1 | -1 | -2 | -2 | -5 |

Half_Even | 6 | 2 | 2 | 1 | 1 | -1 | -1 | -2 | -2 | -6 |

Half_Up | 6 | 3 | 2 | 1 | 1 | -1 | -1 | -2 | -3 | -6 |

Up | 6 | 3 | 2 | 2 | 1 | -1 | -2 | -2 | -3 | -6 |

### Round a number to 2 decimal places with BigDecimal

Whenever you need to round a number to n decimal places, most programmers resort to BigDecimal. Not only does BigDecimal allow you to choose a rounding mode, but it's very easy to do so thanks to this method. Here's an example of rounding a number to 2 decimal places:

floatnumber = BigDecimal.valueOf(digit)

.setScale(2, BigDecimal.ROUND_HALF_DOWN)

.floatValue();

In setScale() you can specify the number of decimal places you want to round, for example, to round up to 5 digits, specify 5. Here we have set the scale as 2 because we round up to 2 digits. We also used rounding mode as **a ROUND_HALF_DOWN**, which is similar to **RoundingMode.HALF_DOWN**.

The rounding mode HALF_DOWN rounds the numbers down if the discarded fraction is 0.5. Instead of BigDecimal.ROUND_HALF_DOWN, you can also use RoundingMode.HALF_DOWN, but just keep in mind that the RoundingMode enumeration was added starting with Java 5.

### Round a number to 2 digits with DecimalFormat

If you are rounding numbers only for display, you should consider using the DecimalFormat or Stringformat method class. While there's nothing wrong with using BigDecimal to round numbers, DecimalFormat seems like the right class for formatting numbers.

Here's the Java code to round a number to 2 significant digits using DecimalFormat in Java.

DecimalFormatdf = newDecimalFormat("#.00");

float number = Float.valueOf(df.format(decimal));

#.00 is designed to round up to 2 decimal places, if you want to round up to 3 or 4 characters, just use #.000 or #.0000 to create a decimal format. To learn more about formatting numbers, see How to Format Numbers in Java Using DecimalFormat.

## Conclusion

It's all about rounding a number to 2 decimal places in Java. You've seen 3 ways to round numbers in Java. BigDecimal looks like the most optimal way to round numbers because of the convenient API and support for various rounding modes.