Toolmingo
Guides7 min read

Percentage Calculator: How to Calculate Percentages (with Formulas and Code)

Learn the three core percentage formulas: what is X% of Y, X is what percent of Y, and percentage change. Includes code examples in JavaScript, Python, Go, and PHP.

Percentage Calculator: How to Calculate Percentages (with Formulas and Code)

Percentages appear in almost every domain — taxes, discounts, grade curves, growth rates, battery levels, and profit margins. Yet it is easy to mix up the three core questions. This guide covers each one with a clear formula, worked examples, and code in four languages.


The Three Core Percentage Questions

Question Formula Example
What is X% of Y? result = Y × (X / 100) 15% of 200 = 30
X is what % of Y? percent = (X / Y) × 100 30 is what % of 200 = 15 %
What is the % change from A to B? change = ((B − A) / A) × 100 200 → 230 = +15 %

Formula 1 — What Is X% of Y?

This is the most common question: finding a part of a whole.

Part = Whole × (Percent / 100)

Examples:

  • 20% of 150 → 150 × 0.20 = 30
  • 8.5% VAT on a €200 purchase → 200 × 0.085 = €17
  • 1% of 1,000,000 → 1,000,000 × 0.01 = 10,000

Real-world use cases: tip amounts, sales tax, commission calculations, discounts, interest.


Formula 2 — X Is What Percent of Y?

You have two numbers and want to know the ratio as a percentage.

Percent = (Part / Whole) × 100

Examples:

  • 45 out of 60 on a test → (45 / 60) × 100 = 75 %
  • 350 customers renewed out of 500 → (350 / 500) × 100 = 70 %
  • Your file is 2.4 MB of a 16 MB limit → (2.4 / 16) × 100 = 15 %

Real-world use cases: grade percentage, conversion rates, capacity usage, market share.


Formula 3 — Percentage Change (Increase or Decrease)

You want to know how much something grew or shrank relative to its starting value.

Percentage change = ((New value − Old value) / Old value) × 100
  • Positive result → increase
  • Negative result → decrease

Examples:

  • Revenue grew from €80,000 to €92,000 → ((92,000 − 80,000) / 80,000) × 100 = +15 %
  • Temperature dropped from 20 °C to 14 °C → ((14 − 20) / 20) × 100 = −30 %
  • Stock price moved from $150 to $150 → 0 %

Real-world use cases: year-over-year growth, A/B test lift, price change, weight loss tracking.


Finding the Original Value

If you know the result of applying a percentage, you can reverse-calculate the original:

Original = Part / (Percent / 100)

Examples:

  • 40 is 25% of what number? → 40 / 0.25 = 160
  • After a 15% rise, a stock costs $92. What was it before? → 92 / 1.15 ≈ $80
  • After a 20% discount, you paid €64. Original price? → 64 / 0.80 = €80

Quick-Reference Table

Percent Of 100 Of 250 Of 1 000
1 % 1 2.5 10
5 % 5 12.5 50
10 % 10 25 100
15 % 15 37.5 150
20 % 20 50 200
25 % 25 62.5 250
33.3 % 33.3 83.25 333.3
50 % 50 125 500
75 % 75 187.5 750

Code Examples

JavaScript

// What is X% of Y?
function percentOf(percent, whole) {
  return Math.round(whole * (percent / 100) * 100) / 100;
}

// X is what percent of Y?
function whatPercent(part, whole) {
  if (whole === 0) throw new Error("Whole cannot be zero");
  return Math.round((part / whole) * 100 * 100) / 100;
}

// Percentage change from A to B
function percentChange(oldVal, newVal) {
  if (oldVal === 0) throw new Error("Old value cannot be zero");
  return Math.round(((newVal - oldVal) / oldVal) * 100 * 100) / 100;
}

console.log(percentOf(15, 200));       // 30
console.log(whatPercent(45, 60));      // 75
console.log(percentChange(80, 92));    // 15
console.log(percentChange(20, 14));    // -30

Python

from decimal import Decimal, ROUND_HALF_UP

def percent_of(percent: float, whole: float) -> Decimal:
    p = Decimal(str(percent)) / 100
    w = Decimal(str(whole))
    return (w * p).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)

def what_percent(part: float, whole: float) -> Decimal:
    if whole == 0:
        raise ValueError("Whole cannot be zero")
    return (Decimal(str(part)) / Decimal(str(whole)) * 100).quantize(
        Decimal("0.01"), rounding=ROUND_HALF_UP
    )

def percent_change(old: float, new: float) -> Decimal:
    if old == 0:
        raise ValueError("Old value cannot be zero")
    return ((Decimal(str(new)) - Decimal(str(old))) / Decimal(str(old)) * 100).quantize(
        Decimal("0.01"), rounding=ROUND_HALF_UP
    )

print(percent_of(15, 200))       # 30.00
print(what_percent(45, 60))      # 75.00
print(percent_change(80, 92))    # 15.00
print(percent_change(20, 14))    # -30.00

Go

package main

import (
	"fmt"
	"math"
)

func round2(v float64) float64 {
	return math.Round(v*100) / 100
}

func percentOf(percent, whole float64) float64 {
	return round2(whole * percent / 100)
}

func whatPercent(part, whole float64) float64 {
	if whole == 0 {
		panic("whole cannot be zero")
	}
	return round2(part / whole * 100)
}

func percentChange(oldVal, newVal float64) float64 {
	if oldVal == 0 {
		panic("old value cannot be zero")
	}
	return round2((newVal - oldVal) / oldVal * 100)
}

func main() {
	fmt.Println(percentOf(15, 200))      // 30
	fmt.Println(whatPercent(45, 60))     // 75
	fmt.Println(percentChange(80, 92))   // 15
	fmt.Println(percentChange(20, 14))   // -30
}

PHP

function percentOf(float $percent, float $whole): float {
    return round($whole * $percent / 100, 2);
}

function whatPercent(float $part, float $whole): float {
    if ($whole == 0) {
        throw new InvalidArgumentException("Whole cannot be zero");
    }
    return round($part / $whole * 100, 2);
}

function percentChange(float $old, float $new): float {
    if ($old == 0) {
        throw new InvalidArgumentException("Old value cannot be zero");
    }
    return round(($new - $old) / $old * 100, 2);
}

echo percentOf(15, 200);      // 30
echo whatPercent(45, 60);     // 75
echo percentChange(80, 92);   // 15
echo percentChange(20, 14);   // -30

Percentage Points vs Percentage Change

This is one of the most common mistakes in statistics and journalism.

Suppose a bank raises its interest rate from 2% to 3%:

  • The rate increased by 1 percentage point (simple arithmetic: 3 − 2 = 1).
  • The rate increased by 50% (percentage change: (3 − 2) / 2 × 100 = 50).

Both statements are correct — they answer different questions. "Percentage point" describes an absolute difference between two percentages. "Percent change" describes the relative growth.


Practical Patterns

Applying a percentage increase

Increase a value by P%:

New value = Old value × (1 + P / 100)

Add 12% VAT to €85: 85 × 1.12 = €95.20

Removing a percentage (reverse)

Strip VAT out of an inclusive price:

Net = Gross / (1 + VAT rate / 100)

Net from €95.20 at 12% VAT: 95.20 / 1.12 = €85

Compound percentage growth

Growth applied repeatedly (e.g., compound interest):

Final = Principal × (1 + rate/100)^n

€1,000 at 5% for 3 years: 1000 × 1.05³ = €1,157.63


Frequently Asked Questions

How do I calculate a percentage of a number?
Multiply the number by the percentage divided by 100. Example: 30% of 250 = 250 × 0.30 = 75.

How do I find what percentage one number is of another?
Divide the part by the whole, then multiply by 100. Example: 15 out of 60 = (15 / 60) × 100 = 25 %.

What is the formula for percentage increase?
((New − Old) / Old) × 100. If sales rose from 400 to 500: ((500 − 400) / 400) × 100 = 25 % increase.

What is the formula for percentage decrease?
The same formula — percentage change. If the result is negative, it is a decrease. Example: 500 → 400: ((400 − 500) / 500) × 100 = −20 %.

Is 50% of 200 the same as 200% of 50?
Yes. 50% of 200 = 100 and 200% of 50 = 100. Percentage multiplication is commutative: A% of B = B% of A.

How do I add a percentage to a number?
Multiply by (1 + percent/100). Adding 20% to 80: 80 × 1.20 = 96. Shortcut: add the percentage amount directly — 20% of 80 is 16, so 80 + 16 = 96.


For instant calculations without writing any code, use the Percentage Calculator — it handles all three formulas and updates in real time as you type.

Related tools

Keep reading

All Toolmingotools are free & run in your browser

No sign-up, no upload, no watermark. Your files never leave your device.

Browse all tools