BuildUtilities

Math & Statistics Guide

Percentages

Percentage calculations come up constantly, discounts, progress bars, conversion rates, tax. Here are the three core formulas:

// What is X% of Y?
result = (X / 100) * Y       // 20% of 150 = 30

// What % is X of Y?
result = (X / Y) * 100       // 30 is 20% of 150

// % change from A to B
result = ((B - A) / A) * 100 // 100 → 130 = +30%

Ratios & Proportions

Ratios express the relationship between two quantities. Common uses: aspect ratios (16:9), mixing recipes, scaling dimensions.

// Simplify a ratio
1920:1080 → 16:9  (divide by GCD of 120)

// Scale proportionally
If 4:3 at 800px wide → height = 800 * (3/4) = 600px

// GCD function (for simplifying)
function gcd(a, b) { return b === 0 ? a : gcd(b, a % b); }

Averages & Central Tendency

Different types of averages tell you different things about your data:

  • Mean: sum ÷ count. Most common average. Sensitive to outliers.
  • Median: middle value when sorted. Better for skewed data (e.g., income).
  • Mode: most frequent value. Useful for categorical data.
Data: [2, 3, 5, 5, 8, 12, 100]

Mean:   19.3  (skewed by the 100)
Median: 5     (middle value, more representative)
Mode:   5     (appears twice)

Age & Date Calculations

Calculating age or date differences seems simple but has edge cases, leap years, timezone boundaries, and month-length differences all add complexity.

// Simple age from birthday
const age = Math.floor(
  (Date.now() - new Date("1990-06-15").getTime())
  / (365.25 * 24 * 60 * 60 * 1000)
);

// Days between two dates
const days = Math.round(
  (new Date("2025-12-31") - new Date("2025-01-01"))
  / (24 * 60 * 60 * 1000)
); // 364

Try our Percentage Calculator, Ratio Calculator, or Average Calculator to crunch numbers instantly.

FAQ

Why does JavaScript have floating-point errors?

JavaScript uses IEEE 754 double-precision floats, so 0.1 + 0.2 ≈ 0.30000000000000004. For money calculations, work in cents (integers) or use a decimal library.

When should I use median instead of mean?

Use median when your data has outliers or is skewed. Salary data is a classic example, a few very high salaries inflate the mean, but the median reflects what most people earn.

Try These Tools

Related Documentation

Tip Jar