Well this one works...

double roundOff = Math.round(a * 100.0) / 100.0;

Output is

123.14

Or as @Rufein said

 double roundOff = (double) Math.round(a * 100) / 100;

this will do it for you as well.

Answer from Bharat Sinha on Stack Overflow
🌐
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.
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));
🌐
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.
🌐
Oreate AI
oreateai.com › blog › how-to-round-to-2-decimal-places-in-java › 18493618794951c7b56c53ccf30f421c
How to Round to 2 Decimal Places in Java - Oreate AI Blog
January 7, 2026 - This code snippet multiplies the original number by 100 (shifting the decimal point two places right), rounds it off, and then divides back by 100 (shifting it back). The result here would be 12.35. However, there’s more than one way to skin this cat! If you're looking for better control over rounding behavior—like specifying whether you want rounding up or down—you can use BigDecimal. This class provides precise control over numerical values and their representations: import java.math.BigDecimal; import java.math.RoundingMode; BigDecimal bd = new BigDecimal(value); bd = bd.setScale(2, RoundingMode.HALF_UP); double finalValue = bd.doubleValue();
🌐
How to do in Java
howtodoinjava.com › home › java basics › round off a float number to 2 decimals in java
Round Off a Float Number to 2 Decimals in Java
February 16, 2024 - BigDecimal is an immutable class and provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The BigDecimal.setScale() method takes two arguments. The first is scale i.e. number of places ...
🌐
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
The result is a string with the value rounded to 2 decimal places. ... The String.format() method is easy and useful when you need to display formatted numbers, but like DecimalFormat, the result is a string and not suitable for further numerical calculations. To make the rounding process reusable, you can create a custom utility method that encapsulates one of the approaches shown above. ... import java.math.BigDecimal; import java.math.RoundingMode; public class Main { public static void main(String[] args) { double value = 12.34567; double roundedValue = roundToNDecimalPlaces(value, 2); System.out.println("Rounded value: " + roundedValue); } public static double roundToNDecimalPlaces(double value, int decimalPlaces) { BigDecimal bd = new BigDecimal(Double.toString(value)); bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP); return bd.doubleValue(); } }
Find elsewhere
🌐
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 - The format specifier %.2f is used within the String.format() method, where %.2f indicates that we want to format the floating-point number with two decimal places. This operation effectively rounds the number to the specified precision.
🌐
Java67
java67.com › 2020 › 04 › 4-examples-to-round-floating-point-numbers-in-java.html
4 Examples to Round Floating-Point Numbers in Java up to 2 Decimal Places | Java67
Half down rounding mode will round numbers down if the discarded fraction is 0.5. In place of BigDecimal.ROUND_HALF_DOWN, you can also use RoundingMode.HALF_DOWN but just beware that RoundingMode enum was added from Java 5 onwards. Of course, you know this because Java Enum was introduced in the 1.5 version. If you are using Java 1.4, then this is the right way to round numbers up to 2 decimals in Java.
🌐
Attacomsian
attacomsian.com › blog › java-round-double-float
Round a double or float number to 2 decimal places in Java
November 26, 2022 - The DecimalFormat class can be used to round a double or floating point value to 2 decimal places, as shown below: double price = 19.5475; DecimalFormat df = new DecimalFormat("0.00"); System.out.println("Price: " + df.format(price)); // Price: ...
🌐
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 - We already covered this method in detail at: Java Math.round() method. Since, we are dealing with two decimal places, you can simply multiply the number by 100, round it to the nearest integer, and then divide by 100.
🌐
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 convert double or float to BigDecimal and use setScale() method to round double/float to 2 decimal places. Here is the example: ... You must be wondering how this works. double*100.0 – 234354.76 Math.round(double*100.0) – 234355.00 (round to nearest value) Math.round(double*100.0)/100.0 ...
🌐
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 ...
🌐
Javatpoint
javatpoint.com › how-to-round-double-and-float-up-to-two-decimal-places-in-java
How to Round Double and Float up to Two Decimal Places in Java - Javatpoint
How to Round Double and Float up to Two Decimal Places in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
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 › 397778 › java › Rounding-Doubles-Decimal-Places
Rounding Doubles to Two Decimal Places (Beginning Java forum at Coderanch)
Originally posted by Joyce Lee: I still don't get the idea why using Math.round approach would end up 10.020000000000001 or 10.0199999999999. I did a quick search in this forum. Here are the threads that recommended using Math.round approach to round off to 2 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 - double : 3.14159265359 double : 3.14 double (RoundingMode.DOWN) : 3.14 double (RoundingMode.UP) : 3.15 ... We also can use String formater / to round the double to 2 decimal places.