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"
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

???
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
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 !!!
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.