Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Percentage and Deciaml conversion  RSS feed

 
Andy Rayburn
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kashyap Hosdurga
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Hope my reply helps you.

Please give more details.

Thanks,
Kashyap
 
Andy Rayburn
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much your reply did help.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.text.DecimalFormat

parse()
to convert 15% to 0.15

format()
to convert 0.15 t0 15%
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!