CyrefJS
CCyrefJS
فا
Back to documentation
Number/Since 1.38.0

percentage

Calculates a percentage of a numeric value.

percentage(value, percent):number

Parameters

valuenumberrequired

The base value from which the percentage is calculated.

percentnumberrequired

The percentage to calculate from the value.

Returns

number

Returns the calculated percentage of the given value.

Errors

Throws a RangeError if value is NaN.

Throws a RangeError if percent is NaN.

Examples

01

Basic usage

Calculates 25% of 200.

TypeScript
const result = percentage(200, 25);

console.log(result);
// 50
02

50 percent

Calculates half of a value.

TypeScript
console.log(percentage(80, 50));
// 40
03

100 percent

100% of a value returns the original value.

TypeScript
console.log(percentage(150, 100));
// 150
04

Zero percent

0% of a value returns 0.

TypeScript
console.log(percentage(500, 0));
// 0
05

Decimal percentage

The percent argument can be a decimal value.

TypeScript
console.log(percentage(200, 12.5));
// 25
06

Negative percentage

Negative percentages are calculated normally.

TypeScript
console.log(percentage(200, -10));
// -20
07

Percentage above 100

Percentages greater than 100 are supported.

TypeScript
console.log(percentage(200, 150));
// 300
08

Negative value

The calculation also works with negative values.

TypeScript
console.log(percentage(-200, 25));
// -50
09

NaN value

A NaN value is rejected.

TypeScript
percentage(NaN, 25);
// RangeError: percentage: value must not be NaN
10

NaN percentage

A NaN percentage is rejected.

TypeScript
percentage(200, NaN);
// RangeError: percentage: percent must not be NaN
Built with@cyref/js·Official documentation