Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert String to float with exact decimal value

 
Narri Dharmis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using BigDecimal.
 
Mukesh Ranjan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use DecimalFormat class
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primitive types don't store trailing 0s. Agree with Rob P.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic