It's not a secret anymore!
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 20131
    
  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: 44538
    
  34
The primitive types don't store trailing 0s. Agree with Rob P.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to convert String to float with exact decimal value
 
It's not a secret anymore!