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
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 › 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.
🌐
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 - To truncate a positive number to two decimal places, we first multiply our double by 100, moving all the numbers we want to keep in front of the decimal place. Next, we use Math.floor() to round down, removing everything after the decimal place.
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - In Java, we can use DecimalFormat("0.00"), String.format("%.2f") or BigDecimal to display double in 2 decimal places.
🌐
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.
🌐
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)
Why are you using DecimalFormat? I thought that was legacy code, superseded by the %f tag. Converting your number to a String and converting it back is simply a way to make additional work and, maybe, to provide additional scope for errors. [edit]Grammatical correction ... The double 0.565 correctly rounds to 0.56.
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
You seem to be hung up on this notion of 2 decimal places, when in fact it's just a formatting issue. If you want to display a double rounded to two decimal points, then use: System.out.printf("%.2f", 10.0); or: System.out.printf("%.2f%n", 10.0); if you want it to act like println().
🌐
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 - DecimalFormat can be used by providing formatting Pattern to format double to 2 decimal places. We can use RoundingMode to specify rounding behavior. Here is an example: ... Double upto 2 decimal places: 2.46 Double upto 2 decimal places – ...
Find elsewhere
🌐
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 - Input : 12.5 Output : 12.500000 Upto 6 decimal places ... 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 ...
🌐
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 - As a result, rounding with these types can lead to subtle truncation or rounding errors. 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.
🌐
Mkyong
mkyong.com › home › java › java – how to round double / float value to 2 decimal places
Java – How to round double / float value to 2 decimal places - Mkyong.com
February 25, 2025 - package com.mkyong.math.rounding; import java.math.BigDecimal; import java.math.RoundingMode; public class BigDecimalExample { public static void main(String[] args) { double input = 1205.6358; System.out.println("Original double value : " + input); // Convert double to BigDecimal BigDecimal salary = new BigDecimal(input); System.out.println("BigDecimal value : " + salary); // Round to 2 decimal places using RoundingMode.HALF_UP BigDecimal salaryRounded = salary.setScale(2, RoundingMode.HALF_UP); System.out.println("Rounded BigDecimal value : " + salaryRounded); // One-line conversion and roun
🌐
Javatpoint
javatpoint.com › two-decimal-places-java
Two Decimal Places Java - Javatpoint
Two Decimal Places Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
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 - The Apache Common library, Math.round(), BigDecimal using the setScale() method, and other tools may all round a double number to two decimal places. Commonly used by programmers “round()” method to round two decimal places to discover ...
🌐
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
Formatting floating point numbers is a common task in software development and Java programming is no different. You often need to pretty print float and double values up-to 2 to 4 decimal places in console, GUI or JSP pages. Thankfully Java provides lots of convenient methods to format a floating ...
🌐
Quora
quora.com › How-do-I-set-decimal-places-in-Java
How to set decimal places in Java - Quora
Answer (1 of 2): In what context? What are you trying to do? If you’re calculating floating point numbers, and just need to display them with a certain number of digits of precision, you can perform the calculations at full scale, and then round to whatever level of precision you need.
🌐
BeginnersBook -
beginnersbook.com › home › java › how to round a number to two decimal places in java
How to round a number to two decimal places in Java
May 31, 2024 - setScale(2, RoundingMode.HALF_UP) sets the scale of the BigDecimal to 2 decimal places.RoundingMode.HALF_UP is the rounding mode that rounds towards “nearest neighbour” unless both neighbours are at equal distance, in which case it rounds up. For example, 2.555 becomes 2.56, and 2.554 becomes ...