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.

Answer from Tassos Bassoukos on Stack Overflow
🌐
Medium
medium.com › @AlexanderObregon › handling-floating-point-precision-in-java-d0f88cc380cc
Handling Floating-Point Precision in Java | Medium
March 19, 2025 - Floating-point numbers work well for general-purpose arithmetic, but they introduce small errors due to the way they store values in binary. In situations where precision matters, such as financial transactions or scientific measurements, Java provides the BigDecimal class as an alternative to double.
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/

Discussions

floating point - How can I handle precision error with float in Java? - Stack Overflow
I'm wondering what the best way to fix precision errors is in Java. As you can see in the following example, there are precision errors: class FloatTest { public static void main(String[] args) ... More on stackoverflow.com
🌐 stackoverflow.com
floating point - Retain precision with double in Java - Stack Overflow
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: More on stackoverflow.com
🌐 stackoverflow.com
When should I use double and when should I use float for Java?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
65
59
January 6, 2024
java - Format Float to n decimal places - Stack Overflow
Are you saying you want to round the float? Normally the only time precision of a float matters is for display. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › java › java numbers › float vs. double in java
Float vs. Double in Java | Baeldung
January 4, 2025 - This test confirms that float uses 4 bytes, while double uses 8 bytes, highlighting the difference in memory requirements between these two data types. Precision defines the number of significant digits a data type can represent accurately, ...
🌐
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 - You can convert a float value to a double without loss of precision. This is possible to do because a float value can hold only a maximum of 7 digits after the decimal, while a double value in Java can hold a maximum of 16 digits after the decimal.
🌐
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.
Find elsewhere
🌐
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.
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
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › lang › Float.html
Float (Java SE 22 & JDK 22)
July 16, 2024 - 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.
🌐
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.
🌐
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 - // 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.
🌐
Educative
educative.io › blog › float-vs-double-in-java
Float vs. double in Java
Precision and range: A float offers roughly 7 decimal digits of precision with a max range of approximately ±3.4×10³⁸, while a double provides about 15–16 decimal digits with a range up to approximately ±1.7×10³⁰⁸. Default type in Java: The compiler treats floating-point literals ...
🌐
ZetCode
zetcode.com › java › float-types
Java Floating-Point Numbers - Complete Guide
Special cases like Double.NaN (never equal to itself, checked with isNaN) and Double.POSITIVE_INFINITY (equal to itself) require distinct handling for safety. For near-zero values like 1e-14, nearlyEqual excels where == falters, ensuring precision in comparisons. This method aligns with Java's strict typing for dependable results. Java's float and double types falter in financial contexts needing exact decimals.
🌐
Medium
tarunjain07.medium.com › java-datatypes-notes-8e70959f4898
float datatypes in Java— [Notes]. **WIP | by Tarun Jain | Medium
September 5, 2022 - The float data type stores a decimal value with a precision of 6–7 digits. 12.12345, for example, is a float that can be kept, whereas 12.123456789 is not. When expressing a float data type in Java, add the letter f to the end of the data ...