🌐
Medium
medium.com › @AlexanderObregon › handling-floating-point-precision-in-java-d0f88cc380cc
Handling Floating-Point Precision in Java | Medium
March 19, 2025 - The double type in Java uses 64 bits: 1 bit for the sign, 11 bits for the exponent, and 52 bits for the mantissa. The mantissa does not store the entire value as a straightforward number but as a fraction of a base-2 system.
🌐
DZone
dzone.com › coding › java › understanding floating-point precision issues in java
Understand Floating-Point Precision Issues in Java - DZone
September 12, 2024 - We lose precision when converting to IEEE format because we consider only the first 32 or 64 bits. This means that the value we have in float or double doesn't represent exactly 0.1. If we convert the IEEE bit array from float to a "real float," ...
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Floating Point Numbers - Lesson | Study.com
October 3, 2018 - Both float and double data types can store positive and negative values. To unlock this lesson you must be a Study.com member Create an account · A float data type in Java stores a decimal value with 6-7 total digits of precision.
🌐
Javatpoint
javatpoint.com › float-vs-double-java
Float Vs Double Java - Javatpoint
Float Vs Double Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Baeldung
baeldung.com › home › java › java numbers › float vs. double in java
Float vs. Double in Java | Baeldung
January 4, 2025 - A float is a 32-bit single-precision floating-point type, occupying four bytes of memory. This compact size makes it well-suited for memory-constrained environments like embedded systems and mobile devices.
🌐
javaspring
javaspring.net › blog › how-can-i-handle-precision-error-with-float-in-java
How to Fix Precision Errors with Float in Java: Causes Explained & Practical Solutions — javaspring.net
Java’s float (32-bit single-precision) and double (64-bit double-precision) follow the IEEE 754 standard for floating-point arithmetic.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Float.html
Float (Java SE 24 & JDK 24)
July 15, 2025 - P where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The Java Language Specification, except that underscores are not accepted between digits. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type float by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.
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
Top answer
1 of 5
26

floats are usually good up to 6 significant digits. Your number is 676543.21 which has 8 significant digits. You will see errors past 6 digits, and those errors will easily propagate to more significant digits the more calculations you perform. If you value your sanity (or precision), use doubles. Floats can't even count past 10 million accurately.

Now, you have 2 significant digits, which suggests to me that there is a chance you want to represent currency - DO NOT . Use your own class that internally represents values using fixed-point arithmetic.

Now, as to Float.MAX_VAL which is 3.4028235E38, meaning 3.4028235*10^38 which is about 10^32 times larger than your value. Notice the 'E' in there? That's the exponent.

2 of 5
9

I love these. But I'll make it quick and painless.

Floats and decimals (aka floating points) aren't kept in the memory precisely either. But I don't want to get into float accuracy vs precision issues here, i'll just point you to a link - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

As far as your other question, lemme put it this way, since floats are stored in scientific notation.

floatVal = 6.765432E6 = 6.7 * 10^6
MAX_VALUE = 3.4E38 = 3.4 * 10^38

MAX_VALUE is 32 orders of magnitude bigger than your float number. Feel free to take a look at here as well http://steve.hollasch.net/cgindex/coding/ieeefloat.html

I've spent a great deal of time comparing and fixing some FP issues a few months ago...

Try to use a small delta when comparing floats. Maybe this link will help you http://introcs.cs.princeton.edu/java/91float/

🌐
ZetCode
zetcode.com › java › float-types
Java Floating-Point Numbers - Complete Guide
Exponential notation (e.g., 2.5e-12) works with both, simplifying work with extreme values. Choose float for constrained memory scenarios and double for precision-critical tasks. Java's floating-point types exhibit precision quirks due to binary storage. This section reveals how small decimals ...
🌐
Medium
prem-parmar.medium.com › headache-free-programming-mastering-precision-with-floating-point-numbers-in-java-ed61a1e92464
Headache-Free Programming: Mastering Precision with Floating-Point Numbers in Java | by Prem Parmar | Medium
April 8, 2023 - Java, like most programming languages, uses the IEEE 754 standard for representing floating-point numbers, which means that the representation of decimal numbers in memory is not exact, but rather an approximation. This can lead to issues when performing arithmetic operations on decimal numbers, particularly when the numbers are very large or very small. The rounding errors can accumulate, leading to unexpected results or a loss of precision.
🌐
W3Schools
w3schools.com › java › java_data_types_numbers.asp
Java Numbers
The precision of float is only 6-7 decimal digits, while double variables have a precision of about 16 digits.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 91float
9.1 Floating Point
September 4, 2024 - As John von Neumann once said "There's no sense in being precise when you don't even know what you're talking about." Java typically prints out floating point numbers with 16 or 17 decimal digits of precision, but do not blindly believe that this means there that many digits of accuracy!
🌐
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 float keyword in Java is a primitive data type, and its size limit is 4 byte or 32 bits (1 byte = 8 bits) single-precision IEEE 754 (Standard for Floating-Point Arithmetic) floating-point number, that is it can allow up to 6 digits precision after the decimal.
🌐
LabEx
labex.io › tutorials › java-how-to-handle-precision-issues-with-floating-point-numbers-in-java-414065
How to handle precision issues with floating-point numbers in Java | LabEx
When dealing with critical applications that require precise floating-point calculations, such as financial, scientific, or engineering systems, it's essential to employ specialized techniques to ensure accurate results. The BigDecimal class in Java provides a way to perform precise decimal arithmetic, avoiding many of the precision issues associated with double and float.
🌐
O'Reilly
oreilly.com › library › view › java-for-dummies › 9781118239742 › a44.html
Floating-point Data Types - Java For Dummies Quick Reference [Book]
June 5, 2012 - The precision of a float type is only about six or seven decimal digits, which isn’t sufficient for most types of calculations. If you use Java to write a payroll system, for example, you might get away with using float variables to store ...
Author: Doug Lowe
Published: 2012
Pages: 288