How can I round a number to n decimal places in Java?
I need a method to round a double in Java using the half-up rounding method, meaning if the digit to be rounded is 5, it always rounds up. Additionally, I want to ensure that only significant digits are displayed—there should be no trailing zeroes.
I have tried String.format("%.5g%n", value)
, but it sometimes retains unnecessary zeroes. I also tested DecimalFormat df = new DecimalFormat("#.#####");
, but it uses half-even rounding instead of half-up.
For example, I want these results:
0.912385 → 0.91239
0.912300 → 0.9123
What is the best approach to achieve this in Java rounding?
If you need precise rounding, BigDecimal is the way to go! I’ve worked with financial calculations, and trust me, this saves you from tricky rounding errors.
Using BigDecimal for Precise Rounding
If you want full control over rounding behavior, BigDecimal
is your best bet. It ensures exact precision, especially when dealing with currency or scientific calculations.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RoundNumber {
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException("Decimal places must be non-negative");
BigDecimal bd = new BigDecimal(value).setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
public static void main(String[] args) {
System.out.println(round(0.912385, 5)); // Output: 0.91239
System.out.println(round(0.912300, 5)); // Output: 0.9123
}
}
Why use this?
Precise control over rounding mode (HALF_UP
ensures 5 rounds up).
No trailing zeroes in the final result.
When to avoid?
If you want a simpler approach without dealing with BigDecimal
, check out the next method.
That’s great for accuracy, @kumari_babitaa , but sometimes, all you need is a clean, formatted output without trailing zeroes. I usually prefer DecimalFormat
when displaying numbers.
Using DecimalFormat for Custom Formatting
If you’re working on formatted output (like UI displays), DecimalFormat
can round numbers while automatically removing unnecessary zeroes.
import java.text.DecimalFormat;
public class RoundNumber {
public static String round(double value) {
DecimalFormat df = new DecimalFormat("#.#####");
return df.format(value);
}
public static void main(String[] args) {
System.out.println(round(0.912385)); // Output: 0.91239
System.out.println(round(0.912300)); // Output: 0.9123
}
}
Why use this?
Automatically removes trailing zeroes.
Simple and effective for formatted output.
When to avoid?
If you need strict precision control for calculations (use BigDecimal
instead).
Both approaches are great, but what if you just need a quick and lightweight way to round numbers in Java? Math.round
does the job with minimal effort!
Using Math.round with Scaling for Simplicity
For a straightforward method without extra libraries, Math.round
can work with a scaling trick.
public class RoundNumber {
public static double round(double value, int places) {
double scale = Math.pow(10, places);
return Math.round(value * scale) / scale;
}
public static void main(String[] args) {
System.out.println(round(0.912385, 5)); // Output: 0.91239
System.out.println(round(0.912300, 5)); // Output: 0.9123
}
}
Why use this?
Lightweight and efficient (uses Math.round
).
Works well for basic rounding needs.
When to avoid?
If you need removal of trailing zeroes, use DecimalFormat
instead.