my dog learned polymorphism*
The moose likes Beginning Java and the fly likes float value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float value" Watch "float value" New topic
Author

float value

Deepa Korecherla
Ranch Hand

Joined: Jul 14, 2004
Posts: 197
This looks silly....but can't figure out. I need to get a float type value from a string value.

Why is the float value rounding up. For example

when i give 123456.488 it results in 123456.484
when i give 988800.45 it results in 988800.44

String sFloatSales = "123456.488";
float fFloatSales1 = Float.parseFloat(sFloatSales);
float fFloatSales2 = Float.valueOf(sFloatSales).floatValue();

Please reply.


DKR<br />SCJP1.2,SCWCD1.4,SCBCD1.3,SCJA
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Float values only have 6 or 7 digits of accuracy anyway, so the differences you see there aren't significant. If they are significant to you then you should be using double instead of float.
 
jQuery in Action, 2nd edition
 
subject: float value
 
Similar Threads
How to convert String to float with exact decimal value
Wrapper-Q
Casting
float to double conversion
Float.valueOf() method and NumberFormatException