Reputation: 45
I've seen similar questions but it's shocking that I didn't see the answer I was, in fact, looking for. So here they are, both the question and the answer:
Q: How to calculate simply the percentage in Python. Say you need a tax calculator. To put it very simple, the tax is 18% of earnings. So how much tax do I have to pay if I earn, say, 18 342? The answer in math is that you divide by 100 and multiply the result by 18 (or multiply with 18 divided by 100). But how do you put that in code?
tax = earnings / 100 * 18
Would that be quite right?
Upvotes: 0
Views: 1828
Reputation: 9
What you did is absolutely correct but I use parenthesis which would be easier to understand.
tax = earnings*(18/100)
Upvotes: 0
Reputation: 45
The answer that best fitted me, especially as it implied no import, was this:
tax = earnings * 0.18
so if I earned 18 342, and the tax was 18%, I should write:
tax = 18 342 * 0.18
which would result in 3 301.56 This seems trivial, I know, and probably some code was expected, moreover this form might be applicable not only in Python, but again, I didn't see the answer anywhere and I thought that it is, after all, the simplest.
Upvotes: 0