Float.valueOf for a String & round decimal

Seventeen Stars

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
Seventeen Stars

Re: Float.valueOf for a String & round decimal

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

??? Smiley Happy
laurent
One Star

Re: Float.valueOf for a String & round decimal

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
Seventeen Stars

Re: Float.valueOf for a String & round decimal

hi Cissine
thanks for complement of informations.

Make some other test , and Float type round value as it want Smiley Happy
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 !!! Smiley Happy
But seem to be the normal way .
@+
laurent
One Star

Re: Float.valueOf for a String & round decimal

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

Re: Float.valueOf for a String & round decimal

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.