You could use BigDecimal.scale() if you pass the number as a String like this:

BigDecimal a = new BigDecimal("1.31");
System.out.println(a.scale()); //prints 2
BigDecimal b = new BigDecimal("1.310");
System.out.println(b.scale()); //prints 3

but if you already have the number as string you might as well just parse the string with a regex to see how many digits there are:

String[] s = "1.31".split("\\.");
System.out.println(s[s.length - 1].length());

Using BigDecimal might have the advantage that it checks if the string is actually a number; using the string method you have to do it yourself. Also, if you have the numbers as double you can't differentiate between 1.31 and 1.310 (they're exactly the same double) like others have pointed out as well.

Answer from Andrei Fierbinteanu 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.
🌐
Coderanch
coderanch.com › t › 681508 › java › maximum-decimal-places-float-double
The maximum decimal places for a float and a double: Where are they exactly defined? (Beginning Java forum at Coderanch)
So I am not an expert, but basically, ... base for the exponent is "10", we don't need to store that. we can just store the "3". now, if we know we have 10 places/bytes/digits to use to store this number, we can decide the first 6 are for the "number" part, and the last four are ...
🌐
Quora
quora.com › How-many-decimal-places-are-in-a-double
How many decimal places are in a double? - Quora
15 decimal digits double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and 52* bits for the value), i.e. double has 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
September 24, 2025 - For example, to keep three decimal places, we’d multiply and divide by 1000. This method is useful if we need to keep our double as a double and not end up converting it to a String.
🌐
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 ...
🌐
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.
🌐
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.
🌐
Tpoint Tech
tpointtech.com › two-decimal-places-java
Two Decimal Places Java - Tpoint Tech
In Java, when we use a double data type before a variable it represents 15 digits after the decimal point.
Find elsewhere
🌐
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 - 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.
🌐
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 ...
🌐
Coderanch
coderanch.com › t › 659719 › java › return-double-decimal-points
How to return double with two decimal points (Java in General forum at Coderanch)
A double (or Double) has no notion of how many "decimal points" it holds; it's simply a value - and often an inexact one at that (read this). So there is no such thing as a "double with two decimal points". I had to redesign the function to return string instead of double...
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));
🌐
Reddit
reddit.com › r/java › java double precision
r/java on Reddit: Java Double Precision
August 24, 2020 -

I came across a piece of code in a legacy Java 8 application at work which adds two doubles and gives out a double. I observed that the resulting doubles for various inputs had variable number of digits after the decimal point. Some were very precise with 12 digits after the decimal point and some had merely a digit after the decimal point.

I’m curious to know what factors affect certain doubles to be so very precise and certain doubles not as much.

Examples:

double one = 3880.95; double two = 380.9; Result: 4261.849999999999

double one = 1293.65; double two = 1293.6; Result: 2587.25

🌐
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 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 point number up to certain decimal places.
🌐
CodingTechRoom
codingtechroom.com › question › how-many-decimal-places-in-double-java
Understanding Decimal Places in a Double Type in Java - CodingTechRoom
A double can represent values with up to 15-17 decimal digits of precision, but the actual number of decimal places shown can vary based on the numeral's value and how it is formatted. ... import java.text.DecimalFormat; public class DecimalPlacesExample { public static void main(String[] args) { double value = 123.456789; DecimalFormat df = new DecimalFormat("0.00"); // This will format the double to 2 decimal places ...
🌐
Coderanch
coderanch.com › t › 753436 › java › count-digits-decimal-point
How to count digits after decimal point (Beginning Java forum at Coderanch)
All non‑integers (real numbers, etc.) have radix points, which is a decimal point in the case of a decimal number. Since doubles are demoninated in binary, their radix points are binary points, and they cannot therefore have any number of places after a decimal point.
🌐
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 - However, there are situations when representing rupees and other units simply call for two decimal places after the decimal point. The Apache Common library, Math.round(), BigDecimal using the setScale() method, and other tools may all round ...