
Anonymous
Not applicable
2012-10-22
07:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
336 Views
5 Replies

Anonymous
Not applicable
2012-10-22
01:18 PM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
hum ..
perhaps the reason is :
???
laurent
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
336 Views

Anonymous
Not applicable
2012-10-23
05:01 AM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
336 Views

Anonymous
Not applicable
2012-10-23
07:18 AM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
336 Views

Anonymous
Not applicable
2013-02-01
02:33 AM
Author
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello
During convertation Float.parseFloat("1.123456789") the result is 1.123 float, but it should be 1.123456789.
Please help with that problem.
During convertation Float.parseFloat("1.123456789") the result is 1.123 float, but it should be 1.123456789.
Please help with that problem.
336 Views

Specialist III
2013-04-26
07:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
336 Views
