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.

Answer from coobird on Stack Overflow
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
🌐
Baeldung
baeldung.com › home › java › java numbers › double precision issue in java
Double Precision Issue in Java | Baeldung
January 8, 2024 - In this short article, we learned what the double precision issue is and how to deal with it. To sum up, rounding error occurs due to the IEEE 754 standard used to represent floating-point numbers.
🌐
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.
🌐
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. ... DOUBLE can be used synonymously with DOUBLE PRECISION. ... These limits are the same as the java.lang.Double Java type limits.
🌐
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.
🌐
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 - The above double number is precise to 7 digits which can easily be seen from the output generated. ... // Java Program to demonstrate // Precision of float import java.io.*; // Driver Class class GFG { // main function public static void main(String[] args) { // Assigned value to f1 Float f1 = 4.024151f; // Assigned value to f2 Float f2 = 2.24525f; Float result = f1 / f2; // Simple division of // two floats f1 and f2.
🌐
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.
Find elsewhere
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Float-vs-Double-Whats-the-difference
Java double vs float: What's the difference?
When Java encounters a decimal in a program, or a calculation generates a floating point number, Java treats the number as a double. For example, both of the following lines of code fail to compile because Java assumes that any decimal is a double.
🌐
DZone
dzone.com › coding › java › understanding floating-point precision issues in java
Understand Floating-Point Precision Issues in Java
September 12, 2024 - Exponent (defines the position of the floating point, with an offset of 127 for float or 1023 for double) Mantissa (the part that comes after the floating point, but is limited by the number of remaining bits) So now converting 0.0001100110011... ...
🌐
The Floating-Point Guide
floating-point-gui.de › languages › java
The Floating-Point Guide - Floating-point cheat sheet for Java
float f = 0.1f; // 32 bit float, note f suffix double d = 0.1d; // 64 bit float, suffix optional · The strictfp keyword on classes, interfaces and methods forces all intermediate results of floating-point calculations to be IEEE 754 values as well, guaranteeing identical results on all platforms. Without that keyword, implementations can use an extended exponent range where available, resulting in more precise results and faster execution on many common CPUs. Java has an arbitrary-precision decimal type named java.math.BigDecimal, which also allows to choose the rounding mode.
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › NUM04-J.+Do+not+use+floating-point+numbers+if+precise+computation+is+required
NUM04-J. Do not use floating-point numbers if precise computation is required - SEI CERT Oracle Coding Standard for Java - Confluence
The Java language provides two primitive floating-point types, float and double, which are associated with the single-precision 32-bit and double-precision 64-bit format values and operations specified by IEEE 754 [IEEE 754]. Each of the floating-point types has a fixed, limited number of mantissa ...
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Double.html
Double (Java SE 24 & JDK 24)
July 15, 2025 - Values of the float type have 24 bits of precision and values of the double type have 53 bits of precision. Therefore, since 0.1 is a repeating fraction in base 2 with a four-bit repeat, 0.1f != 0.1d. In more detail, including hexadecimal floating-point literals:
🌐
Medium
medium.com › @AlexanderObregon › handling-floating-point-precision-in-java-d0f88cc380cc
Handling Floating-Point Precision in Java | Medium
March 19, 2025 - Mantissa (or significand) — Holds the actual digits of the number in binary form. The double type in Java uses 64 bits: 1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa.
🌐
Wikipedia
en.wikipedia.org › wiki › Double-precision_floating-point_format
Double-precision floating-point format - Wikipedia
January 9, 2026 - Version 1.2 allowed implementations to bring extra precision in intermediate computations for platforms like x87. Thus a modifier strictfp was introduced to enforce strict IEEE 754 computations. Strict floating point has been restored in Java 17. As specified by the ECMAScript standard, all arithmetic in JavaScript shall be done using double...
🌐
Apache Commons
commons.apache.org › proper › commons-math › javadocs › api-3.6.1 › org › apache › commons › math3 › util › Precision.html
Precision (Apache Commons Math 3.6.1 API)
... Returns true if the arguments ... are equal or within the range of allowed error (inclusive). Two float numbers are considered equal if there are (maxUlps - 1) (or fewer) floating point numbers between them, i.e....
🌐
Apache
db.apache.org › derby › docs › 10.1 › ref › rrefsqljdoubleprecision.html
DOUBLE PRECISION
The DOUBLE PRECISION data type provides 8-byte storage for numbers using IEEE floating-point notation. ... DOUBLE can be used synonymously with DOUBLE PRECISION. ... These limits are different from the java.lang.DoubleJava type limits.