It's not that you're actually getting extra precision - it's that the float didn't accurately represent the number you were aiming for originally. The double is representing the original float accurately; toString is showing the "extra" data which was already present.

For example (and these numbers aren't right, I'm just making things up) suppose you had:

float f = 0.1F;
double d = f;

Then the value of f might be exactly 0.100000234523. d will have exactly the same value, but when you convert it to a string it will "trust" that it's accurate to a higher precision, so won't round off as early, and you'll see the "extra digits" which were already there, but hidden from you.

When you convert to a string and back, you're ending up with a double value which is closer to the string value than the original float was - but that's only good if you really believe that the string value is what you really wanted.

Are you sure that float/double are the appropriate types to use here instead of BigDecimal? If you're trying to use numbers which have precise decimal values (e.g. money), then BigDecimal is a more appropriate type IMO.

Answer from Jon Skeet on Stack Overflow
Top answer
1 of 11
146

It's not that you're actually getting extra precision - it's that the float didn't accurately represent the number you were aiming for originally. The double is representing the original float accurately; toString is showing the "extra" data which was already present.

For example (and these numbers aren't right, I'm just making things up) suppose you had:

float f = 0.1F;
double d = f;

Then the value of f might be exactly 0.100000234523. d will have exactly the same value, but when you convert it to a string it will "trust" that it's accurate to a higher precision, so won't round off as early, and you'll see the "extra digits" which were already there, but hidden from you.

When you convert to a string and back, you're ending up with a double value which is closer to the string value than the original float was - but that's only good if you really believe that the string value is what you really wanted.

Are you sure that float/double are the appropriate types to use here instead of BigDecimal? If you're trying to use numbers which have precise decimal values (e.g. money), then BigDecimal is a more appropriate type IMO.

2 of 11
58

I find converting to the binary representation easier to grasp this problem.

float f = 0.27f;
double d2 = (double) f;
double d3 = 0.27d;

System.out.println(Integer.toBinaryString(Float.floatToRawIntBits(f)));
System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d2)));
System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d3)));

You can see the float is expanded to the double by adding 0s to the end, but that the double representation of 0.27 is 'more accurate', hence the problem.

   111110100010100011110101110001
11111111010001010001111010111000100000000000000000000000000000
11111111010001010001111010111000010100011110101110000101001000
🌐
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.
Published: May 23, 2026
Discussions

java - Why converting from float to double changes the value? - Stack Overflow
The value of a float does not change when converted to a double. There is a difference in the displayed numerals because more digits are required to distinguish a double value from its neighbors, which is required by the Java documentation. More on stackoverflow.com
🌐 stackoverflow.com
How float is converted to double in java? - Stack Overflow
I write the following code in java and check the values stored in the variables. when I store 1.2 in a double variable 'y' it becomes 1.200000025443 something. Why it is not 1.200000000000 ? public More on stackoverflow.com
🌐 stackoverflow.com
March 8, 2015
java - How to round a float value to double value? - Stack Overflow
The float values are in a one part of a complex model. The formatting layer use only double. Also DecimalFormat accept only double and not float. ... If the result is expressed appropriately for humans seems, to me, only to be relevant when the value is presented to humans through println(). More on stackoverflow.com
🌐 stackoverflow.com
Why does adding long to double as the result we got double?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. 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/learnjava
12
6
August 8, 2022
🌐
Baeldung
baeldung.com › home › java › java numbers › how to convert double to float in java
How to Convert Double to Float in Java | Baeldung
April 19, 2024 - In this tutorial, we’ll discuss double and float and learn how to convert them into each other. float is a 32-bit single-precision floating type and can store around 7 decimal digits. This data type has an advantage when there is a need to save memory.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Convert Between Double and Float in Java - Java Code Geeks
March 18, 2024 - Pay attention to the ‘f’ suffix appended to the float values. We’ll explore two techniques for converting between double and float data types in Java—Type Casting and using Class methods.
🌐
Simplesolution
simplesolution.dev › java-convert-float-to-double
Convert float to double in Java - Simple Solution
With a Float object in Java we can use Float.doubleValue() method to return double value of it.
🌐
Way2Java
way2java.com › home › float to double java
float to double Java
November 23, 2015 - float to double conversion is an implicit conversion, 4 bytes to 8 bytes, implicitly done by JVM. Example, Screenshot given in Simple terms for a Beginner
Find elsewhere
Top answer
1 of 10
17

The value of a float does not change when converted to a double. There is a difference in the displayed numerals because more digits are required to distinguish a double value from its neighbors, which is required by the Java documentation. That is the documentation for toString, which is referred (through several links) from the documentation for println.

The exact value for 125.32f is 125.31999969482421875. The two neighboring float values are 125.3199920654296875 and 125.32000732421875. Observe that 125.32 is closer to 125.31999969482421875 than to either of the neighbors. Therefore, by displaying “125.32”, Java has displayed enough digits so that conversion back from the decimal numeral to float reproduces the value of the float passed to println.

The two neighboring double values of 125.31999969482421875 are 125.3199996948242045391452847979962825775146484375 and 125.3199996948242329608547152020037174224853515625.
Observe that 125.32 is closer to the latter neighbor than to the original value (125.31999969482421875). Therefore, printing “125.32” does not contain enough digits to distinguish the original value. Java must print more digits in order to ensure that a conversion from the displayed numeral back to double reproduces the value of the double passed to println.

2 of 10
12
  1. When you convert a float into a double, there is no loss of information. Every float can be represented exactly as a double.
  2. On the other hand, neither decimal representation printed by System.out.println is the exact value for the number. An exact decimal representation could require up to about 760 decimal digits. Instead, System.out.println prints exactly the number of decimal digits that allow to parse the decimal representation back into the original float or double. There are more doubles, so when printing one, System.out.println needs to print more digits before the representation becomes unambiguous.
🌐
Baeldung
baeldung.com › home › java › java numbers › float vs. double in java
Float vs. Double in Java | Baeldung
January 4, 2025 - This test checks that when a value exceeds the lower bound of the float‘s range, it correctly underflows to zero, as expected for values too small to be represented. Choosing between float and double in Java requires understanding the trade-offs in precision, memory usage, and application needs.
Top answer
1 of 2
3

This is a question of formatting above anything else.

Take a look at the Float.toString documentation (Float.toString is what's called to produce the decimal representations you see for the floats, and Double.toString for the double):

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type float. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument f. Then f must be the float value nearest to x; or, if two float values are equally close to x, then f must be one of them and the least significant bit of the significand of f must be 0.

(emphasis mine)

The situation is the same for Double.toString. But, you need more digits to "uniquely distinguish the argument value from adjacent values of type double" than you do for float (recall that double is 64-bits while float is 32), that's why you're seeing the extra digits for double and not for float.

Note that anything that can be represented by float can also be represented by double, so you're not actually losing any precision in the conversion.

Of course, not all numbers can be exactly representable by float or double, which is why you see those seemingly random extra digits in the decimal representation in the first place. See "What Every Computer Scientist Should Know About Floating-Point Arithmetic".

2 of 2
1

The reason why there's such issue is because a computer works only in discrete mathematics, because the microprocessor can only represent internally full numbers, but no decimals. Because we cannot only work with such numbers, but also with decimals, to circumvent that, decades ago very smart engineers have invented the floating point representation, normalized as IEEE754.

The IEEE754 norm that defines how floats and doubles are interpreted in memory. Basically, unlike the int which represent an exact value, the floats and doubles are a calculation from:

  • sign
  • exponent
  • fraction

So the issue here is that when you're storing 1.2 as a double, you actually store a binary approximation to it:

00111111100110011001100110011010

which gives you the closest representation of 1.2 that can be stored using a binary fraction, but not exactly that fraction. In decimal fraction, 12*10^-1 gives an exact value, but as a binary fraction, it cannot give an exact value.

(cf http://www.h-schmidt.net/FloatConverter/IEEE754.html as I'm too lazy to do it myself)

when I store 1.2 in a double variable 'y' it becomes 1.200000025443 something

well actually in both the float and the double versions of y, the value actually is 1.2000000476837158, but because of the smaller mantissa of the float, the value represented is truncated before the approximation, making you believe it's an exact value, whereas in the memory it's not.

🌐
Coderanch
coderanch.com › t › 374719 › java › converting-float-double
converting float to double (Java in General forum at Coderanch)
October 4, 2004 - Here's some sample code: public static void testFloatToDouble() { Float f = new Float(18.4); Double d = new Double(f.floatValue()); System.out.println("Float: " + f); System.out.println("Float as double: " + f.doubleValue()); System.out.println("Float as Double: " + d); } And the results: Float: 18.4 Float as double: 18.399999618530273 Float as Double: 18.399999618530273 Jon ... This is a consequence of the way floating-point numbers are stored in computers (per IEEE 754 standards). If the decimal portion cannot be expressed as a sum of reciprocal powers of 2 within a particular "window," then the number loses precision. If you want precision, then you should use the BigDecimal class in java.math. But be sure to use the String constructor, or you'll get similar errors.
🌐
Educative
educative.io › blog › float-vs-double-in-java
Float vs. double in Java
Precision and range: A float offers ... compiler treats floating-point literals as double by default, so assigning one to a float variable requires an explicit cast or the f suffix to avoid a compilation error....
🌐
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 - As the Float consumes less memory in comparison to the Double data type. By default, Java treats any decimal or float numbers as double type, so in case, if we want to define any value as float, we need to manually typecast it using the 'f' or 'F' keyword in the suffix of the number.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Float vs. Double - Lesson | Study.com
July 21, 2018 - To unlock this lesson you must be a Study.com member Create an account · This lesson has provided an overview of float, which holds 4 bytes of memory (32 bits) and stores about 7 digits, and double, which can hold 8 bytes (64 bits) and store about 15 digits after the decimal point, data types in Java.
🌐
Coderanch
coderanch.com › t › 378176 › java › float-double-conversion
float to double conversion (Java in General forum at Coderanch)
When showing a double this code finds the even closer value of 3.0199999809265137 (not present in the 'world of float') and shows it. If this hunch is correct then this entire situation might be as close to correct as possible on the machine level, but might introduce an intuitive 'gotcha'. If this is a correct assumption then there's no real problem with using widening conversion in Java between float and double, but it does imply to remember that what you see is not necessarily what you've got (just very close to it).
🌐
Medium
medium.com › @ujjawalr › what-happens-when-you-compare-float-and-double-in-java-51f236a6d89c
What happens when you compare float and double in Java? | by Ujjawal Rohra | Medium
September 8, 2025 - That means many simple decimal ... binary numeric promotion (if needed) and then compare. If one operand is double, the other is converted to double first....
🌐
Blogger
programmingjungle.blogspot.com › 2013 › 03 › float-to-double-conversion-in-java.html
Programming Jungle: Float to double conversion in Java
March 30, 2013 - How accurate our approximation is depends how many bytes we use to represent it. That is why you may be surprised about the result of the flowing code: float fvar=0.1f; double dvar=(double)fvar; System.out.print("Are these number equal?
🌐
Dot Net Perls
dotnetperls.com › float-java
Java - float Number - Dot Net Perls
This is a floating-point, 4-byte number. In Java a float is like a double but only has 4 bytes (a double has twice as many bytes). With this type, we can store numbers with a decimal place. A float can be cast to a double without any special syntax.
🌐
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 - It is rarely appropriate to use this constructor. All MethodsStatic MethodsInstance MethodsConcrete Methods ... Returns the value of this Float as a byte after a narrowing primitive conversion. ... Compares the two specified float values. ... Compares two Float objects numerically. ... Returns an Optional containing the nominal descriptor for this instance, which is the instance itself. ... Returns the value of this Float as a double after a widening primitive conversion.