Win a copy of Spring Boot in Practice this week in the Spring forum!
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Percentage and Deciaml conversion

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
OK, I am new to Java. I am trying to figur out how to convert a percentage such as 15% to a decimal like .15 and then converting it back. I have done some serching but I think I am looking and asking for the wrong thing.

Thanks

Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
Andy,
Can you please give more details?
If you are reading the value from STD i/p or calculating it from some other parameters.

If you are asking the user to i/p then it is pretty simple.
You can store it in a double or float variable and divide by 100.
And to convert it back you can multiply by 100 and put a % when you display.

Thanks,
Kashyap

Andy Rayburn
Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 4632
• Number of slices to send:
Optional 'thank-you' note:
java.text.DecimalFormat

parse()
to convert 15% to 0.15

format()
to convert 0.15 t0 15%