| 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: 19232
|
|
|
Try using BigDecimal.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
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: 32830
|
|
|
The primitive types don't store trailing 0s. Agree with Rob P.
|
 |
 |
|
|
subject: How to convert String to float with exact decimal value
|
|
|