Number methods

Methods carried by every number: range mapping, limiting and fixed-precision formatting — plus the number() global for parsing strings.

Every mJS number carries these built-in methods — they chain naturally for sensor scaling pipelines:

let waterLevel = 9; // feet
let pct = waterLevel.map(0, 18, 0, 100, true) // scale 0–18 ft → 0–100 %
                    .toFixed(1);              // "50.0"

Functions

number.map()

x.map(in_min, in_max, out_min, out_max, [limit]) → number

Linearly scales the number from one range to another, with optional clamping to the output range.

The method form of Math.map() — maps the value from the input range to the output range. The optional limit argument clamps the result to the output range; if not specified the output is not limited.

Arguments

NameTypeRequiredDefaultDescription
in_min number yes — Lower bound of the input range.
in_max number yes — Upper bound of the input range.
out_min number yes — Lower bound of the output range.
out_max number yes — Upper bound of the output range.
limit boolean optional false true to clamp the result to the output range.

Returns

number The mapped (and optionally clamped) value.

Example

let waterLevel = 9; // feet

// Map the reservoir level (0–18 ft) to a 0–100 percentage,
// clamped to the output range by the optional true argument.
let reservoirPercentage = waterLevel.map(0, 18, 0, 100, true);
// reservoirPercentage = 50

number.limit()

x.limit(min, max) → number

Clamps the number between a lower and upper bound.

Arguments

NameTypeRequiredDefaultDescription
min number yes — Lower bound.
max number yes — Upper bound.

Returns

number The value clamped to [min, max].

Example

let reservoirPercentage = 103;
let limited = reservoirPercentage.limit(0, 100);
// limited = 100

number.toFixed()

x.toFixed([decimals]) → string

Formats the number as a string with a fixed number of decimal places.

Arguments

NameTypeRequiredDefaultDescription
decimals number optional 0 Number of decimal places.

Returns

string The formatted value.

Example

let pressure = 123.34224322;
let stringValue = pressure.toFixed(2); // "123.34"