Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

For example:

round(200.3456, 2); // returns 200.35

Original version; watch out with this

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

I've been using the above to round "not-too-big" doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it's best to avoid it, since more reliable ways are readily available, with cleaner code too.

So, use this instead

(Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = BigDecimal.valueOf(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

Note that HALF_UP is the rounding mode "commonly taught at school". Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

Of course, if you prefer, you can inline the above into a one-liner:
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

And in every case

Always remember that floating point representations using float and double are inexact. For example, consider these expressions:

999199.1231231235 == 999199.1231231236 // true
1.03 - 0.41 // 0.6200000000000001

For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

System.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

Some excellent further reading on the topic:

  • Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch
  • What Every Programmer Should Know About Floating-Point Arithmetic

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

Answer from Jonik on Stack Overflow
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-double-precision-2-decimal-places-example-float-range-math-jvm
Java double decimal precision
The precision of a double in Java is 10-324 decimal places, although true mathematical precision can suffer due to issues with binary arithmetic.
Top answer
1 of 13
962

Here's an utility that rounds (instead of truncating) a double to specified number of decimal places.

For example:

round(200.3456, 2); // returns 200.35

Original version; watch out with this

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

This breaks down badly in corner cases with either a very high number of decimal places (e.g. round(1000.0d, 17)) or large integer part (e.g. round(90080070060.1d, 9)). Thanks to Sloin for pointing this out.

I've been using the above to round "not-too-big" doubles to 2 or 3 decimal places happily for years (for example to clean up time in seconds for logging purposes: 27.987654321987 -> 27.99). But I guess it's best to avoid it, since more reliable ways are readily available, with cleaner code too.

So, use this instead

(Adapted from this answer by Louis Wasserman and this one by Sean Owen.)

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = BigDecimal.valueOf(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

Note that HALF_UP is the rounding mode "commonly taught at school". Peruse the RoundingMode documentation, if you suspect you need something else such as Bankers’ Rounding.

Of course, if you prefer, you can inline the above into a one-liner:
new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue()

And in every case

Always remember that floating point representations using float and double are inexact. For example, consider these expressions:

999199.1231231235 == 999199.1231231236 // true
1.03 - 0.41 // 0.6200000000000001

For exactness, you want to use BigDecimal. And while at it, use the constructor that takes a String, never the one taking double. For instance, try executing this:

System.out.println(new BigDecimal(1.03).subtract(new BigDecimal(0.41)));
System.out.println(new BigDecimal("1.03").subtract(new BigDecimal("0.41")));

Some excellent further reading on the topic:

  • Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch
  • What Every Programmer Should Know About Floating-Point Arithmetic

If you wanted String formatting instead of (or in addition to) strictly rounding numbers, see the other answers.

Specifically, note that round(200, 0) returns 200.0. If you want to output "200.00", you should first round and then format the result for output (which is perfectly explained in Jesper's answer).

2 of 13
403

If you just want to print a double with two digits after the decimal point, use something like this:

double value = 200.3456;
System.out.printf("Value: %.2f", value);

If you want to have the result in a String instead of being printed to the console, use String.format() with the same arguments:

String result = String.format("%.2f", value);

Or use class DecimalFormat:

DecimalFormat df = new DecimalFormat("####0.00");
System.out.println("Value: " + df.format(value));
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
It is a common requirement to format currencies to two decimal places. You can easily achieve this with the Java printf function. Just use %.2f as the format specifier. This will make the Java printf format a double to two decimal places.
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - We can use DecimalFormat("0.00") to ensure the number is round to 2 decimal places. ... package com.mkyong.math.rounding; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalExample { private static final DecimalFormat ...
🌐
Java2Blog
java2blog.com › home › number › format double to 2 decimal places in java
Format Double to 2 Decimal Places in Java [ 7 Ways ] - Java2Blog
November 8, 2023 - Double upto 2 decimal places: 2.46 Double upto 2 decimal places – RoundingMode.DOWN: 2.45 Double upto 2 decimal places – RoundingMode.UP: 2.46 · DecimalFormat provides different ways to explicitly set rounding behavior using RoundingMode.. For example, we have used RoundingMode.UP and RoundingMode.DOWN to format double based on different rounding mode.
🌐
ONEXT DIGITAL
onextdigital.com › home › easy ways to round doubles in java to two decimal places
Easy ways to round doubles in java to two decimal places
July 19, 2023 - public class Example { public static void main(String[] args) { double dbl = 5215.1246; System.out.println("Original Double value: "+dbl); System.out.println("Updated rounded Double value is: "+String.format("%.2f", dbl)); } } ... linuxhint@DESKTOP-XXXXXX:~$ java Example Original Double value: 5215.1246 Updated rounded Double value is: 5215.12 · Overall, rounding doubles in Java to two decimal places can be useful for improving readability, reducing errors, and saving resources when working with financial or other types of data that require high precision.
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
The IEEE floating-point format that Java uses has binary fractions, not decimal ones, so it would be impossible to return a "double with 2 decimal places" for the same reason that you can't return 1/3 to 2 decimal places. What you'd get back would be inaccurate.
Find elsewhere
🌐
Arrowhitech
blog.arrowhitech.com › format-double-to-2-decimal-places-java
Format double to 2 decimal places java: Effective ways to implement it – Blogs | AHT Tech | Digital Commerce Experience Company
To print double to two decimal places, you can utilize the static method format() in String. System.out.printf and this method are similar. ... This is the best way to print double to two decimal places on console if you want to print double to two decimal places.
🌐
Baeldung
baeldung.com › home › java › java numbers › how to round a number to n decimal places in java
How to Round a Number to N Decimal Places in Java | Baeldung
September 24, 2025 - For example, the value 260.775 cannot be exactly represented as a double. Internally, it might be stored as slightly less than 260.775, so rounding it to two decimal places results in 260.77 instead of 260.78. These inaccuracies stem from how floating-point numbers are stored in memory. Floating-point values are stored as a combination of a mantissa and an exponent, which allows them to represent a wide range of numbers but at the cost of precision.
🌐
Delft Stack
delftstack.com › home › howto › java › how to round a double to two decimal places in java
How to Round a Double to Two Decimal Places in Java | Delft Stack
February 12, 2024 - This demonstrates the successful rounding of the original number to two decimal places using DecimalFormat, providing a user-friendly and formatted output while maintaining numerical precision. In Java, the Apache Commons Math library provides a reliable and efficient way to round a double to a specific number of decimal places.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-precision-for-double-values-in-java
How to Set Precision For Double Values in Java? - GeeksforGeeks
July 12, 2025 - We can use the format() method of the String class to format the decimal number to some specific format. ... // Java Program to Illustrate format() Method // of String class // Importing required classes import java.io.*; import java.lang.*; ...
🌐
Coderanch
coderanch.com › t › 744280 › java › double-decimal-places-Java
Trying to round up double for two decimal places using Java 1.8? (Java in General forum at Coderanch)
That method is subject to the usual imprecision always seen with floating‑point arithmetic. If the absolute value of 100 * x exceeds the precision of the double datatype (approx. 15.9 decimal digits), the rounding will have no visible effect. Yes, you can use a BigDecimal and round with the CEILING rounding mode.
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
You can use any of these technique to pretty print any float or double variable in Java. I personally like to use DecimalFormat for its readability advantage but like SimpleDateFormat, this is also an expensive object and not thread-safe, so use this with caution. One more thing to consider is trailing zeros, if you want trailing zeros e.g. want to print 2 as "2.00" then use String class' format method, otherwise use DecimalFormat's format method. Most of the time you can use a local instance of DecimalFormat, but if performance is critical for your application then you either need to explicitly synchronize access of this object or use a ThreadLocal variable, which is more efficient, and avoids cost of acquiring and releasing locks.
🌐
Baeldung
baeldung.com › home › java › java numbers › truncate a double to two decimal places in java
Truncate a Double to Two Decimal Places in Java | Baeldung
September 24, 2025 - The String.format() method takes two arguments. Firstly, the format we want to apply, and secondly, the arguments referenced by the format. To truncate to two decimal places, we’ll use the format String “%.2f”...
🌐
Javaprogramto
javaprogramto.com › 2021 › 11 › java-double-2-decimal-places.html
Java Format Double - Double With 2 Decimal Points Examples JavaProgramTo.com
Double value : 9.144678376262 Number format : 9.14 Formatter : 9.14 printf : Double upto 2 decimal places: 9.14