Percent of a Number
"What is 15% of 80?", "12 is what percent of 48?" and "30 is 40% of what?" are the three basic percent questions. They all come from one equation, . This lesson solves each kind, shows mental shortcuts, and computes discounts and tips exactly with Decimal.
One equation, three questions
Whichever of the three numbers is missing, rearrange to find it:
| Question | Missing | Formula | Example |
|---|---|---|---|
| What is 15% of 80? | part | ||
| 12 is what % of 48? | rate | ||
| 30 is 40% of what? | whole |
Tip: "of" means multiply and "is" means equals. "30 is 40% of what?" translates to , so .
Mental shortcuts
- 10%: move the decimal point one place left. .
- 5%: half of 10%. .
- 1%: move the point two places left. .
- Build others: ; ; 25% = a quarter.
- Swap trick: . 8% of 50 is hard; 50% of 8 is 4, and they are equal.
The three questions in code
def percent_of(pct, whole):
return pct / 100 * whole
def what_percent(part, whole):
return part / whole * 100
def whole_from(part, pct):
return part / (pct / 100)
print("15% of 80 =", percent_of(15, 80))
print("12 of 48 =", what_percent(12, 48), "%")
print("30 is 40% of ", whole_from(30, 40))
print("8% of 50 =", percent_of(8, 50), " 50% of 8 =", percent_of(50, 8))15% of 80 = 12.0
12 of 48 = 25.0 %
30 is 40% of 75.0
8% of 50 = 4.0 50% of 8 = 4.0
Discounts and tips
A discount of removes of the price, so you pay of it. A tip or tax of adds , so you pay . Multiplying by one factor is quicker than computing the part and then adding or subtracting:
For money, use Decimal and round to cents with an explicit rule:
from decimal import Decimal, ROUND_HALF_UP
CENT = Decimal("0.01")
def apply_pct(amount, pct):
"""Change an amount by pct percent (negative = discount)."""
factor = 1 + Decimal(pct) / 100
return (Decimal(amount) * factor).quantize(CENT, rounding=ROUND_HALF_UP)
print("jacket, 25% off:", apply_pct("64.00", "-25"))
print("meal, 18% tip: ", apply_pct("46.50", "18"))
print("coffee 5.10, 15% tip:", apply_pct("5.10", "15"))
# the same with floats
print(5.10 * 1.15, f"{5.10 * 1.15:.2f}")jacket, 25% off: 48.00
meal, 18% tip: 54.87
coffee 5.10, 15% tip: 5.87
5.864999999999999 5.86
The exact value is , a tie, which rounds half up to 5.87. The float product is 5.864999…, just below the tie, so formatting it with two places gives 5.86. A one-cent bug like this, repeated over millions of invoices, is exactly why money code uses Decimal.
Finding the original price
A shirt costs 36 after a 20% discount. What was the original price? It is not . The 20% was taken off the original, so 36 is 80% of the original:
Check: 20% of 45 is 9, and . ✓
In software
- E-commerce: discounts, coupons, VAT/sales tax, and "price before tax" from a gross price (divide by ).
- Resource limits: "alert when disk use passes 90%", Kubernetes CPU requests as percentages of a core, rate limiters allowing 5% of traffic to a canary.
- Sampling: logging 1% of requests means keeping a request when
random() < 0.01.
Exercises
Exercise 1: Part of a whole
Find 35% of 260 using the 10% / 5% shortcuts.
Answer
91
Solution
Exercise 2: What percent?
A 1.5 GB download has finished 0.6 GB. What percent is done?
Answer
40%
Solution
.
Exercise 3: Find the whole
A 12% commission earned a salesperson 540. What were the total sales?
Answer
4500
Solution
, so . Check: .
Exercise 4: Price before tax
A receipt shows a gross price of 59.50 including 19% VAT. Write Python with Decimal that finds the net price and the VAT amount, rounded to cents.
Answer
Net 50.00, VAT 9.50.
Solution
, so . .
from decimal import Decimal, ROUND_HALF_UP
gross = Decimal("59.50")
net = (gross / Decimal("1.19")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print("net:", net, " VAT:", gross - net)net: 50.00 VAT: 9.50
Test yourself
What is 30% of 70?
- 21
- 2.1
- 210
- 23.3
18 is what percent of 72?
- 18%
- 40%
- 25%
- 4%
After a 25% discount a game costs 45. What was the original price?
- 56.25
- 60
- 33.75
- 70