r/learnjava • u/Stinovlas • Apr 19 '20
-20%
Hello, I am starting in Java and I would be happy if you could help me. I was searching on google but I could not find any video or article about this. I only found how to divide or how much is 50% out of 200
I learned how to subtract or divide but I want to make something like this 500-200=300-(20%) in one button click. I mean how to subtract % of a number.
I would be very happy if you could help me or if you know a video that can solve my problem.
Regards Stinovlas :)
2
Upvotes
7
u/FrenchFigaro Apr 19 '20
Percentages are fractions.
If you want to subtract or add percentages, you need to figure out what percentage of the initial number the result is.
For example, if you want to subtract 30%, the result is 70% of the initial number, so the result is
yourInitialNumber * (70 / 100)If you want to add 10%, the result is 110% of the initial number, so the result is
yourInitialNumber * (110 / 100)Now, I voluntarily selected numbers other than your 20% so that you can think about it from the examples.
Another thing you have to think of, is the type of your data. Here, 70 / 100 is an integer division, which will not be accurate. You'll need to figure out how to use float or double division.