How to round a number to n decimal places in Java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Rounding numbers to a specific number of decimal places is a common task in Java programming, especially when dealing with calculations that require a certain precision. Java provides several ways to round numbers, including using the BigDecimal class, the Math class methods, and string formatting options. This article provides a comprehensive guide on these methods, explaining their usage and advantages.
Using BigDecimal for Precision
One of the most reliable methods to round numbers in Java is by using the BigDecimal class, which offers excellent control over the scale and rounding mode.
Example:
In this example, setScale takes two arguments: the first is the new scale (number of decimal places), and the second is the rounding mode. RoundingMode.HALF_UP is one of the rounding modes defined in the RoundingMode enum, which rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case it rounds up.
Rounding Using Math Class
If you are dealing with primitive data types like double or float, Java's Math class provides several methods for rounding:
Math.round(): It is useful for rounding to the nearest whole number. It returnsintorlong, depending on the input type (floatordouble).Math.ceil(): This method rounds up to the nearest whole number.Math.floor(): This method rounds down to the nearest whole number.
Example:
In this code, number is multiplied by 100 to shift the decimal two places to the right, rounded to the nearest whole number, and then divided by 100 to shift the decimal back to its original position.
Formatting Numbers with String.format
Another way to round numbers while converting them into Strings is by using String.format(). This method allows you to specify the format, including the number of decimal places.
Example:
In the format string %.2f, .2 specifies the number of decimal places, and f indicates a floating-point number.
Summary Table
| Method | Use Case | Pros | Cons |
| BigDecimal | High precision calculations | Most precise and configurable | Slower due to object allocation |
| Math class | Quick rounding of primitives | Faster for simple tasks | Less control over rounding behavior |
| String.format | Rounding while converting to string | Convenient for formatted output | Involves type conversion, slightly slower |
Best Practices and Additional Tips
- Choose the right tool: Use
BigDecimalfor financial and precise calculations where exact decimal representation is crucial. UseMathclass methods for less critical calculations to gain performance. - Understand Rounding Modes: Different rounding modes can lead to substantially different results, especially in financial computations.
- Prefer String.format for UI: When rounding is intended for display purposes,
String.formator similar text formatting tools are preferable.
By understanding these different methods and choosing the right one for your context, you can handle rounding in Java effectively and efficiently.

