Skip to content

ratio() ​

Import from
@gabreusi/hyrax
Examples
Run, answers asserted
ts
function ratio(
   value: number, 
   max?: number, 
   min?: number
): number;

Defined in: core/number.ts:77

Where value sits inside the range [min, max], as a fraction: 0 at min, 1 at max. It is meant to be multiplied by other values to keep proportions. It does not clamp, so the result can be negative or above 1. An empty range (max === min) gives 0.

Parameters ​

ParameterTypeDefault valueDescription
valuenumberundefinedThe value to locate.
maxnumber100The upper end of the range.
minnumber0The lower end of the range.

Returns ​

number

The fraction of the range that value represents.

Example ​

ts
ratio(50);            // => 0.5
ratio(30, 60);        // => 0.5
ratio(75, 100, 50);   // => 0.5

MIT License. Every example on these pages is type-checked against the built package, and the core ones are run.