wood burning stoves
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
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: 20079
    
  30

Try using BigDecimal.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 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: 44003
    
  33
The primitive types don't store trailing 0s. Agree with Rob P.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: How to convert String to float with exact decimal value
 
It's not a secret anymore!