percent
The percent utility calculates what percentage value is of total, returning a number on the 0–100 scale. Returns 0 when total is 0 to avoid division-by-zero errors.
Source Code
View Source Code
ts
/**
* Calculates what percentage `value` is of `total`.
*
* @example
* ```ts
* percent(25, 100); // 25
* percent(1, 3); // 33.333...
* percent(50, 200); // 25
* percent(0, 100); // 0
* percent(5, 0); // 0
* ```
*
* @param value - The partial value.
* @param total - The total value.
*
* @returns The percentage (0–100 scale). Returns 0 when `total` is 0.
*/
export function percent(value: number, total: number): number {
if (total === 0) return 0;
return (value / total) * 100;
}Features
- Zero-safe: Returns
0whentotalis0instead ofInfinityorNaN. - 0–100 scale: Standard percentage output, not a 0–1 fraction.
- Composable: Pair with
roundfor display formatting.
API
ts
function percent(value: number, total: number): number;Parameters
value: The partial value.total: The whole or reference total.
Returns
- The percentage as a number on the 0–100 scale.
Examples
Basic Usage
ts
import { percent } from '@vielzeug/toolkit';
percent(25, 100); // 25
percent(1, 3); // 33.333...
percent(50, 200); // 25
percent(0, 100); // 0
percent(5, 0); // 0 — zero-safeWith Rounding
ts
import { percent, round } from '@vielzeug/toolkit';
round(percent(1, 3), 2); // 33.33
round(percent(2, 3), 1); // 66.7Progress Bar
ts
import { percent, clamp, round } from '@vielzeug/toolkit';
function progressBar(current: number, total: number): string {
const pct = round(clamp(percent(current, total), 0, 100), 1);
return `${pct}%`;
}
progressBar(45, 200); // '22.5%'
progressBar(200, 200); // '100%'Completion Stats
ts
import { percent, round } from '@vielzeug/toolkit';
const tasks = [{ done: true }, { done: false }, { done: true }, { done: true }, { done: false }];
const completed = tasks.filter((t) => t.done).length;
round(percent(completed, tasks.length), 0); // 60