Just cast your double to a float.
Copydouble d = getInfoValueNumeric();
float f = (float)d;
Also notice that the primitive types can NOT store an infinite set of numbers:
Copyfloat range: from 1.40129846432481707e-45 to 3.40282346638528860e+38
double range: from 1.7e–308 to 1.7e+308
Answer from Tom Wellbrock on Stack OverflowJust cast your double to a float.
Copydouble d = getInfoValueNumeric();
float f = (float)d;
Also notice that the primitive types can NOT store an infinite set of numbers:
Copyfloat range: from 1.40129846432481707e-45 to 3.40282346638528860e+38
double range: from 1.7e–308 to 1.7e+308
Convert Double to Float
Copypublic static Float convertToFloat(Double doubleValue) {
return doubleValue == null ? null : doubleValue.floatValue();
}
Convert double to Float
Copypublic static Float convertToFloat(double doubleValue) {
return (float) doubleValue;
}
Videos
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.
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
I am learning Java, and am confuse on when to choose double or float for my real numbers or int. It feels like, it doesn’t matter because from my limited experience (with Java) both of them deliver the same results, but I don’t want to go further down the learning curve with Java and have a bad habit of using either messing up my code, and not having a clue as to why. So, when should you use float and double?
From my textbook:
When you write a floating-point literal in your program code, Java assumes it to be of the double data type. A double value is not compatible with a float variable because a double can be much larger or smaller than the allowable range for a float. As a result, code such as the following will cause an error:
Example 1:
float number; number = 23.5; //Error!
You can force a double to be treated as a float, by suffixing it with the letter F or f. The preceding code can be rewritten in the following manner to prevent an error:
Example 2:
float number; number = 23.5F; //This will work. ===================================================
I can't figure out why example 1 produces an error. Why is 23.5 considered a double? I thought it was a float.
AFAIK, float numbers are precise up to 7 decimal digits of accuracy. 23.5 only has 1 decimal digit. What am I not understanding? I thought by typing 'float number;' you are declaring the number variable to be a float. Why would you need the extra step of adding an F?
Thanks