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
963

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));
Discussions

[Java] Rounding a double to two decimal places
System.out.printf("%.2f", 4.567823); More on reddit.com
🌐 r/learnprogramming
3
0
April 28, 2013
How do I round a double to two decimal places in Java? - Stack Overflow
This works great if the amount ... to 2 decimal places. ... Sign up to request clarification or add additional context in comments. ... he multiplies the number by 100(so 651.517 becomes 65151.7...) and rounds it off to the nearest 1(so it becomes 65152) and then divides it back by 100(651.52). 2013-07-16T21:17:39.24Z+00:00 ... You should write 100.0 not 100, otherwise it will treat it as int. 2014-04-08T10:18:26.933Z+00:00 ... @Anatoly No - Java evaluates ... More on stackoverflow.com
🌐 stackoverflow.com
round up to 2 decimal places in java? - Stack Overflow
Just pass your number to this function as a double, it will return you rounding the decimal value up to the nearest value of 5; More on stackoverflow.com
🌐 stackoverflow.com
How to round up to 2 decimal places in this code?
If you don't need anything fancy, I'd say the most straightforward method is String.format() , e.g.: System.out.println(String.format("Length in metres is %.2f", (total_inches * 2.54)/100)); More on reddit.com
🌐 r/javahelp
4
3
October 5, 2020
🌐
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 ...
🌐
Mkyong
mkyong.com › home › java › java – display double in 2 decimal places
Java - Display double in 2 decimal places - Mkyong.com
October 29, 2021 - package com.mkyong.math.rounding; import java.math.RoundingMode; import java.text.DecimalFormat; public class DecimalExample { private static final DecimalFormat df = new DecimalFormat("0.00"); public static void main(String[] args) { double input = 3.14159265359; System.out.println("double : " + input); System.out.println("double : " + df.format(input)); //3.14 // DecimalFormat, default is RoundingMode.HALF_EVEN df.setRoundingMode(RoundingMode.DOWN); System.out.println("\ndouble (RoundingMode.DOWN) : " + df.format(input)); //3.14 df.setRoundingMode(RoundingMode.UP); System.out.println("double (RoundingMode.UP) : " + df.format(input)); //3.15 } }
🌐
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
March 18, 2026 - 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 ...
🌐
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 - To round a double to two decimal places, we can leverage this method by multiplying the original value by 100, rounding it to the nearest integer, and then dividing it back by 100.
Find elsewhere
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Round to 2 Decimal Places in Java - Lesson | Study.com
January 5, 2018 - Create your account · In summary, when we have long, complicated numbers (lots of numbers to the right of the decimal point), it's helpful to round. This helps the end user conceptualize the number. We can use the Math.round function in Java ...
🌐
BeginnersBook
beginnersbook.com › 2024 › 05 › 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 - public class RoundExample { public static void main(String[] args) { double doubleValue = 143.456789; float floatValue = 143.456789f; String roundedDouble = String.format("%.2f", doubleValue); String roundedFloat = String.format("%.2f", floatValue); // Output: 143.46 System.out.println("Rounded double: " + roundedDouble); // Output: 143.46 System.out.println("Rounded float: " + roundedFloat); } } In this approach, we create an instance of DecimalFormat with the pattern #.00. The pattern #.00 indicates that the number should have at least one digit before the decimal point and exactly two digit
🌐
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
1 month ago - These unexpected results occur because primitive types like float and double use binary representations, which can’t exactly represent some decimal values. 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.
🌐
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
You can use printf and the %f specifier to format a double to two decimal places of precision. Binary numbers don’t always map cleanly to the base-10 number system. As a result, there can sometimes be a loss of precision in even the simplest ...
🌐
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)
July 19, 2021 - To expand on what we before, a floating‑point number which cannot be exactly defined as multiples of ½, ¼, ⅛, etc., cannot be defined as a double, and the exact equality test will fail. jshell> new BigDecimal(199.95) $2 ==> 199.94999999999998863131622783839702606201171875 The imprecision ...
🌐
Sentry
sentry.io › sentry answers › java › round a number to n decimal places in java
Round a Number to N Decimal Places in Java | Sentry
January 15, 2025 - The following example creates a ... args) { double value = 12.34567; DecimalFormat df = new DecimalFormat("#.##"); System.out.println("Rounded value: " + df.format(value)); } }...
🌐
YouTube
youtube.com › shorts › ylLBHcXFT2o
Rounding a Double to Two Decimal Digits with java.lang.Math #java #shorts - YouTube
Using Math.round (https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Math.html#round(double)) to round to two decimal places.See you at l
Published   July 23, 2023
🌐
Attacomsian
attacomsian.com › blog › java-round-double-float
Round a double or float number to 2 decimal places in Java
November 26, 2022 - The Math.round() method is another way to round a double or floating point number to 2 decimal places:
🌐
Java2Blog
java2blog.com › home › math › java round double/float to 2 decimal places
java round double/float to 2 decimal places - Java2Blog
May 12, 2021 - You can use DecimalFormat too to round number to 2 decimal places. ... You can convert double or float to BigDecimal and use setScale() method to round double/float to 2 decimal places.
🌐
Verve AI
vervecopilot.com › interview-questions › what-does-mastering-java-round-double-to-2-decimal-places-reveal-about-your-professional-aptitude
What Does Mastering Java Round Double To 2 Decimal Places… | Verve AI
August 28, 2025 - Cons: Requires manual scaling (multiplying and dividing by 100.0); primarily for computational rounding, not for strict formatting. Code Example: ```java double value = 123.45678; double roundedValue = Math.round(value * 100.0) / 100.0; // Result: ...
🌐
Intellipaat
intellipaat.com › home › blog › how to round to two decimal places in java?
How to round to two decimal places in java?
February 26, 2026 - We can use Math.round() function to print the value up to 2 values in Java. To use the round function, we should multiply the num by 100.0 and then divide by 100.0. This function is easy to use and mostly used by developers to round off a decimal ...