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, getInfoValueNumeric()
, it's of double
type. The value we get is in the 2.3423424666767E13
form.
So how do we get a float
format data like 23423424666767
?
2.3423424666767E13 to 23423424666767
public void setInfoValueNumeric(java.lang.Double value) {
setValue(4, value);
}
@javax.persistence.Column(name = "InfoValueNumeric", precision = 53)
public java.lang.Double getInfoValueNumeric() {
return (java.lang.Double) getValue(4);
}
The problem is, your value cannot be stored accurately in single precision floating point type. Proof:
Another thing is: you don't get "2.3423424666767E13", it's just the visual representation of the number stored in memory. "How you print out" and "what is in memory" are two distinct things. Example above shows you how to print the number as float, which avoids scientific notation you were getting.
You can use a decimal formatter for formatting decimal numbers.
Output : 23423424666767
Converting from double to float will be a narrowing conversion. From the doc:
So it is not a good idea. If you still want it you can do it like:
Float.parseFloat(String.valueOf(your_number)
Convert
Double
toFloat
Convert
double
toFloat
Just cast your double to a float.
Also notice that the primitive types can NOT store an infinite set of numbers: