Two Laptop Bag*
The moose likes Java in General and the fly likes How to convert String to float with exact decimal value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to convert String to float with exact decimal value" Watch "How to convert String to float with exact decimal value" New topic
Author

How to convert String to float with exact decimal value

Narri Dharmis
Greenhorn

Joined: Nov 10, 2008
Posts: 6
Hi,

When I convert a String value to float by using Float.valueOf(String), The return value is not containing last decimal.

Ex: String a = 2.50;
float b = Float.valueOf(a);

Now the float "b" is holding only upto 2.5. It is not converting String value to float properly. My requirement is to convert a String value to Float with exact decimals.

Please tell me how can I convert a String to float with complete decimals.

Thanks
Naresh.D
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Try using BigDecimal.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mukesh Ranjan
Greenhorn

Joined: Jun 24, 2009
Posts: 22
use DecimalFormat class
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
The primitive types don't store trailing 0s. Agree with Rob P.
 
Don't get me started about those stupid light bulbs.
 
subject: How to convert String to float with exact decimal value
 
Similar Threads
Float.valueOf is rounding 999999.99 to 1000000.00
Conversion problem
double
Converting String value 2.0 into int
Float.valueOf() method and NumberFormatException