Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Float.valueOf for a String & round decimal

hi all
we've some "strange" rounding behavior with reading a String and convert to a Float value.
from an Xml file, we insert 456193.15 as a String (mysql table).
When I read it & convert it to a Float (with dedicated component or Float.valueOf method) it "rounded" to 456193.16.
Agree for some value like ......,98 or .....,99 !
is it a normal behavior, even if I know that Float doesn't keep "precision"
thanks for advice
regards
laurent
Labels (3)
5 Replies
Anonymous
Not applicable
Author

hum ..
perhaps the reason is :
Rounding errors: Not every decimal number can be expressed exactly as a floating point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit

??? 0683p000009MACn.png
laurent
Anonymous
Not applicable
Author

Hi Laurent !
Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29
Mohcine
Anonymous
Not applicable
Author

hi Cissine
thanks for complement of informations.

Make some other test , and Float type round value as it want 0683p000009MACn.png
As preconize Big Decimal is the only way to be keep value as it ....
I was suprising about rouding some values (456193.15) that dont' look like to be rounded (oups !!! 0683p000009MACn.png
But seem to be the normal way .
@+
laurent
Anonymous
Not applicable
Author

Hello
During convertation Float.parseFloat("1.123456789") the result is 1.123 float, but it should be 1.123456789.
Please help with that problem.
_AnonymousUser
Specialist III

Hi I have also experienced this issue and found it only affected large numbers (100K +) and discovered that using the talend type of double has corrected it.