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
145

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
57

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
🌐
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.
Discussions

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
Casting from Float to Double
Where can I find a write up about how to cast variables correctly? When I cast from float to double the variable contains random numbers in the added precision. for example: float a =1.234567 double b = 0.0 b = (double)a; then b is displayed as 1.23456704632568 Thanks More on forums.ni.com
🌐 forums.ni.com
0
May 9, 2011
convert float to double
For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you More on forums.oracle.com
🌐 forums.oracle.com
September 17, 2008
Convert double to float in Java - Stack Overflow
I am facing an issue related to converting double to float. Actually, I store a float type, 23423424666767, in a database, but when we get data from the database in the below code, getInfoValueNume... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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?
–Dell Technologies & Intel® · Infrastructure for Machine Learning, AI Requirements, Examples –TechTarget · The key difference between a float and double in Java is that a double can represent much larger numbers than a float.
🌐
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.
🌐
National Instruments
forums.ni.com › community › discussion forums › most active software boards › labwindows/cvi › casting from float to double
Casting from Float to Double - NI Community
May 9, 2011 - The result you are getting is expected behavior. A float only has 6 digits of precision and cannot represent 1.234567 exactly. When the number is type casted to a double, you are seeing the additional numbers that were already present.
Find elsewhere
🌐
Oracle
forums.oracle.com › ords › apexds › post › convert-float-to-double-3719
convert float to double
September 17, 2008 - Hi, I want to put a float value to double variable. float f = 1.0000345f; double d = f; System.out.println("float " + f);//Prints 1.0000345 System.out.println("double " + d);//P...
🌐
GeeksforGeeks
geeksforgeeks.org › java › double-floatvalue-in-java-with-examples
Double floatValue() in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java Program to Implement floatValue() Method // of Double Class // Class class GFG { // Main driver method public static void main(String[] args) { // Taking a double value by // creating object of Double class Double d = new Double(-1023.15); ...
🌐
W3Schools
w3schools.com › java › java_type_casting.asp
Java Type Casting
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Type casting means converting one data type into another. For example, turning an int into a double. ... Widening Casting (automatic) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double
🌐
Processing Forum
forum.processing.org › two › discussion › 2341 › double-to-float-conversion.html
Double to float conversion - Processing 2.x and 3.x Forum
... Poersch already answered that question. The float() method can only take an int, not a double. I was wrong when I said it could take a double. It can only take an int, a String, or an array.
🌐
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 - 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.
🌐
Coderanch
coderanch.com › t › 564234 › java › Precision-loss-String-Float-double
Precision loss in String to Float and double to float (Beginning Java forum at Coderanch)
January 11, 2012 - My friend Bear Bibeault This forum as I can see is related to java only. JavaRanch » Java Forums » Beginning Java ... Well the real problem was a wrapper around this. We used XSD->Jaxb-> java classes XSD has data type xs:float mentioned in it, for which JAXB uses float type in java.
🌐
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 - In addition, this class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a float. This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The class java.lang.Double has a discussion of equality, equivalence, and comparison of floating-point values that is equally applicable to float values.
🌐
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.
🌐
Simplesolution
simplesolution.dev › java-convert-float-to-double
Convert float to double in Java
With a Float object in Java we can use Float.doubleValue() method to return double value of it.
🌐
FavTutor
favtutor.com › blogs › java-float-vs-double
Java: Float vs Double | 4 Main Differences (& when to use?) | FavTutor
February 23, 2021 - This loss of bits from the end of the number is very pronounced while using float. The number of bits lost is much lesser while using double because of the large number of bits allocated for the exponent. Default: Double is the default data type used by Java for dealing with fractional numbers. To force Java to use the float data type, the character ‘f’ or ‘F’ must be suffixed to the number.
🌐
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).
🌐
Quora
quora.com › How-can-I-round-off-a-float-double-to-single-decimal-point-in-Java
How to round off a float/double to single decimal point in Java - Quora
Answer (1 of 3): There is a method java.lang.Math.round() which can be used to round-off numbers to desired decimal point(s)… This line of code rounds-off a double to 1 decimal point- [code]double roundOff = Math.round(a * 10.0) / 10.0; [/code]Hope, it helps…