Percentage Calculator
Why This Is Harder Than It Looks
Percentage math seems trivial until the edge cases. A 50% loss then 50% gain doesn't break even. Going from 4% to 6% is "2 percentage points" or "50% relative increase" -- confusing them gets roasted in a code review.
Four modes because "percentage" covers four operations:
- X is what percent of Y -- proportion. 347/420 lines = 82.6% coverage.
- What is X percent of Y -- part extraction. 30% of $149 = $44.70.
- Percentage change -- growth/decline. ((new-old)/old)x100.
- Reverse percentage -- find original before change.
Using the wrong mode is the #1 source of percentage bugs.
The Four Modes
Mode 1: X is what percent of Y -- (X / Y) x 100. Test coverage, completion rate. 347/420 -> 82.62%.
Mode 2: What is X percent of Y -- Y x (X / 100). Discounts, tips. 30% of $149 = $44.70. Final = $104.30.
Mode 3: Percentage change -- ((Y - X) / X) x 100. MoM growth. 4.2M -> 4.83M API calls = +15%.
Mode 4: Reverse percentage -- final / (1 + rate). $96K after 20% raise = $96K / 1.20 = $80K base.
The Multiplicative Property Problem
People expect percentages to stack. They don't -- they compound:
- $200 with 10% off then 10% on = $200 x 0.9 x 1.1 = $198. Down $2.
- 50% loss then 50% gain = $100 x 0.5 x 1.5 = $75. Down 25%.
- Two 20% increases = $1 x 1.2 x 1.2 = $1.44. +44%, not +40%.
This trips people in pricing, investment returns, A/B tests. Chain changes -- multiply factors, not percentages.
Percentage Points vs Relative Percent
"Interest rates from 4% to 6%." Relative: "up 50%." Absolute: "up 2 percentage points." Both true; intent differs.
Financial Presets
- Markup x cost = selling price. $18 at 45% markup = $26.10.
- Margin vs markup: Margin = % of selling price; markup = % of cost. 50% margin = 100% markup.
- VAT: Total / (1 + rate). $120 at 20% VAT = $100 net.
Practical Examples
Dashboard. p99: 120ms -> 100ms = -16.7%. Show the negative. Coverage. 347/420 = 82.6%. Ship. A/B lift. Control 3.3% vs variant 3.8% = +15.2%. Pair with t-test. Budget. $50K -> $62K = +$12K, +24%. Show both.
Division by Zero and Traps
- Change from 0: undefined. Report delta, not %.
- 99% bug: 100 - 1% = 99. 99 + 1% = 99.99. Asymmetric.
FAQ
Q: CAGR? A: Percentage change, then (end/start)^(1/n) - 1. Q: Margin vs markup? A: 50% margin = 100% markup on cost. Q: Negative percentages? A: Yes. Sign preserves directionality.