File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Head First Android 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: 20041
    
  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: 43292
    
  32
The primitive types don't store trailing 0s. Agree with Rob P.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert String to float with exact decimal value
 
It's not a secret anymore!