• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How to convert String to float with exact decimal value

 
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
 
Sheriff
Posts: 22040
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using BigDecimal.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use DecimalFormat class
 
Marshal
Posts: 71028
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primitive types don't store trailing 0s. Agree with Rob P.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic