You can try BigDecimal for this purpose

Double toBeTruncated = new Double("3.5789055");

Double truncatedDouble = BigDecimal.valueOf(toBeTruncated)
    .setScale(3, RoundingMode.HALF_UP)
    .doubleValue();
Answer from Neel on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ double precision issue in java
Double Precision Issue in Java | Baeldung
January 8, 2024 - According to the standard, the representation of a double-precision data type consists of three parts: Sign bit โ€“ contains the sign of the number (1 bit) Exponent โ€“ controls the scale of the number (11 bits) Fraction (Mantissa) โ€“ contains the significant digits of the number (52 bits) Now, to understand the double precision issue, letโ€™s perform a simple addition of two decimal numbers:
๐ŸŒ
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

๐ŸŒ
Oracle
docs.oracle.com โ€บ javadb โ€บ 10.10.1.2 โ€บ ref โ€บ rrefsqljdoubleprecision.html
DOUBLE PRECISION data type
The DOUBLE PRECISION data type provides 8-byte storage for numbers using IEEE floating-point notation.
๐ŸŒ
Medium
medium.com โ€บ @sachinkg12 โ€บ why-double-loses-precision-and-how-to-avoid-it-in-java-19066974ddd7
Why double Loses Precision and How to Avoid It in Java | by Sachin Gupta | Medium
January 22, 2025 - The double data type in Java follows the IEEE 754 standard for floating-point arithmetic. It represents numbers in binary format using: ... Finite Precision: double can only represent numbers up to 15โ€“17 decimal digits accurately.
Top answer
1 of 16
183

As others have mentioned, you'll probably want to use the BigDecimal class, if you want to have an exact representation of 11.4.

Now, a little explanation into why this is happening:

The float and double primitive types in Java are floating point numbers, where the number is stored as a binary representation of a fraction and a exponent.

More specifically, a double-precision floating point value such as the double type is a 64-bit value, where:

  • 1 bit denotes the sign (positive or negative).
  • 11 bits for the exponent.
  • 52 bits for the significant digits (the fractional part as a binary).

These parts are combined to produce a double representation of a value.

(Source: Wikipedia: Double precision)

For a detailed description of how floating point values are handled in Java, see the Section 4.2.3: Floating-Point Types, Formats, and Values of the Java Language Specification.

The byte, char, int, long types are fixed-point numbers, which are exact representions of numbers. Unlike fixed point numbers, floating point numbers will some times (safe to assume "most of the time") not be able to return an exact representation of a number. This is the reason why you end up with 11.399999999999 as the result of 5.6 + 5.8.

When requiring a value that is exact, such as 1.5 or 150.1005, you'll want to use one of the fixed-point types, which will be able to represent the number exactly.

As has been mentioned several times already, Java has a BigDecimal class which will handle very large numbers and very small numbers.

From the Java API Reference for the BigDecimal class:

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue ร— 10^-scale).

There has been many questions on Stack Overflow relating to the matter of floating point numbers and its precision. Here is a list of related questions that may be of interest:

  • Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
  • How to print really big numbers in C++
  • How is floating point stored? When does it matter?
  • Use float or decimal for accounting application dollar amount?

If you really want to get down to the nitty gritty details of floating point numbers, take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.

2 of 16
111

When you input a double number, for example, 33.33333333333333, the value you get is actually the closest representable double-precision value, which is exactly:

33.3333333333333285963817615993320941925048828125

Dividing that by 100 gives:

0.333333333333333285963817615993320941925048828125

which also isn't representable as a double-precision number, so again it is rounded to the nearest representable value, which is exactly:

0.3333333333333332593184650249895639717578887939453125

When you print this value out, it gets rounded yet again to 17 decimal digits, giving:

0.33333333333333326
๐ŸŒ
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.
๐ŸŒ
Codersarts
codersarts.com โ€บ forum โ€บ java-programming-help โ€บ java-programming-help-how-to-set-precision-for-double-value
Java Programming Help: How to set Precision for double value | Codersarts
August 10, 2019 - You can set Precision for double value either via BigDecimal or DecimalFormat, depending on what you want to do with the value later. ... package com.codersarts; import java.text.DecimalFormat; public class DecimalFormatExample { // setting precision upto 2 decimal point private static ...
Find elsewhere
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 442773 โ€บ java โ€บ Precision-double-literals
Precision for double literals (Java in General forum at Coderanch)
Java uses the IEEE standard for floating points -- 32 bit and 64 bit standard. Henry OK, I see that in the JLS. But, as I understand it*, it specifies how floating point numbers are encoded into binary. Using this encoding it's not possible to precisely specify the number 0.9 (for example).
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java numbers โ€บ java double keyword
Java double keyword
February 19, 2025 - The double type is twice as large as float: 8 bytes versus 4. It is also called a double precision real number. Of the 64 bits reserved for a double number, 1 is signed bit, 11 bits are for exponent and 52 bits are for significand.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ float vs. double in java
Float vs. Double in Java | Baeldung
January 4, 2025 - Java provides two primitive data types for floating-point arithmetic: float and double. Both adhere to the IEEE 754 standard, ensuring consistent behavior across platforms. However, their size, precision, and performance differ significantly.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ double
double Keyword in Java: Usage & Examples
The double keyword in Java is a primitive data type that represents a double-precision 64-bit IEEE 754 floating point.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ float vs double in java - difference you should know
Float Vs Double in Java - Difference You Should Know - Scaler Topics
March 27, 2024 - The double keyword in Java is also a primitive data type, and its size limit is 8 byte or 64 bits double-precision IEEE 754 floating-point number, that is it can allow up to 15 digits precision after the decimal.
๐ŸŒ
DZone
dzone.com โ€บ data engineering โ€บ data โ€บ why you should never use float and double for monetary calculations
Why You Should Never Use Float and Double for Monetary Calculations
August 21, 2018 - For example, 12.345 has the precision of 5 (total digits) and the scale of 3 (number of digits right of the decimal). We might get exponentiation in the calculation result if we do not follow some best practices while using BigDecimal. Below is the code snippet that shows a helpful usage example of handling the calculation result with BigDecimal. ... import java.math.BigDecimal; public class BigDecimalForCurrency { public static void main(String[] args) { int scale = 4; double value = 0.11111; BigDecimal tempBig = new BigDecimal(Double.toString(value)); tempBig = tempBig.setScale(scale, BigDecimal.ROUND_HALF_EVEN); String strValue = tempBig.stripTrailingZeros().toPlainString(); System.out.println("tempBig = " + strValue); } }
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 392988 โ€บ java โ€บ double-precision
double precision? (Beginning Java forum at Coderanch)
double a=4190.04d; double b=4771.24d; double total =0d; total=total+a+b=0d+4190.04d+4771.24d=8961.28d as you wish.But the result 8961.2799999999 is what the double precision make sense!Double type always keep the precision fragment as long as it can.In other words,8961.28d equal to 8961.2799999999.
Top answer
1 of 4
14

@PeterLawrey states max precision in 15.

That's actually not what he stated at all. What he stated was:

double has 15 decimal places of accuracy

and he is wrong. They have 15 decimal digits of accuracy.

The number of decimal digits in any number is given by its log to the base 10. 15 is the floor value of log10(253-1), where 53 is the number of bits of mantissa (including the implied bit), as described in the Javadoc and IEEE 754, and 253-1 is therefore the maximum possible mantissa value. The actual value is 15.954589770191003298111788092734 to the limits of the Windows calculator.

He is quite wrong to describe it as 'decimal places of accuracy'. A double has 15 decimal digits of accuracy if they are all before the decimal point. For numbers with fractional parts you can get many more than 15 digits in the decimal representation, because of the incommensurability of decimal and binary fractions.

2 of 4
2

Run this code, and see where it stops

public class FindPrecisionDouble {
  static public void main(String[] args) {
    double x = 1.0;
    double y = 0.5;
    double epsilon = 0;
    int nb_iter = 0;
    while ((nb_iter < 1000) && (x != y)) {
        System.out.println(x-y);
        epsilon = Math.abs(x-y);
        y = ( x + y ) * 0.5;
    }
    final double prec_decimal = - Math.log(epsilon) / Math.log(10.0);
    final double prec_binary = - Math.log(epsilon) / Math.log(2.0);
    System.out.print("On this machine, for the 'double' type, ");
    System.out.print("epsilon = " );
    System.out.println( epsilon );
    System.out.print("The decimal precision is " );
    System.out.print( prec_decimal );
    System.out.println(" digits" );
    System.out.print("The binary precision is " );
    System.out.print( prec_binary );
    System.out.println(" bits" );
  }
}

Variable y becomes the smallest value different than 1.0. On my computer (Mac Intel Core i5), it stops at 1.1102...E-16. It then prints the precision (in decimal and in binary).

As stated in https://en.wikipedia.org/wiki/Machine_epsilon, floating-point precision can be estimated with the epsilon value. It is "the smallest number that, when added to one, yields a result different from one" (I did a small variation: 1-e instead of 1+e, but the logic is the same)

I'll explain in decimal: if you have a 4-decimals precision, you can express 1.0000 - 0.0001, but you cannot express the number 1.00000-0.00001 (you lack the 5th decimal). In this example, with a 4-decimals precision, the epsilon is 0.0001. The epsilon directly measures the floating-point precision. Just transpose to binary.

Edit Your question asked "How to determine...". The answer you were searching were more an explanation of than a way to determine precision (with the answer you accepted). Anyways, for other people, running this code on a machine will determine the precision for the "double" type.

๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ understanding floating-point precision issues in java
Understand Floating-Point Precision Issues in Java
September 12, 2024 - More precisely, instead of 0.1, we get 0.100000001490116119384765625. How can we verify this? There are a couple of ways. Take a look at the following code: ... Now that we know the value we see at initialization is different from what is actually stored in float/double, we expect the value on the left (0.1) but instead, we initialize with the value on the right (0.100000001490116119384765625):
๐ŸŒ
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.
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java
Double Precision Issue in Java - Java Code Geeks
February 14, 2024 - However, when comparing the sum to 0.3, which would be the expected result of adding 0.1 and 0.2, you might expect the program to print sum is equal to 0.3. However, due to the nature of floating-point arithmetic in different versions of Java, the comparison sum == 0.3 could be evaluated as false due to precision issues. ... When dealing with floating-point numbers in Java, itโ€™s essential to be aware of the double precision issue, which can lead to inaccuracies in calculations due to limited precision.