Numbers
Arithmetic helpers. A result that is not a finite number (for example sqrt(-1)) is a NON_FINITE error rather than NaN. formatNumber and formatCurrency produce text for display: separators and spacing come from the locale data of the JavaScript runtime (some locales use a non-breaking space, and runtimes may differ), so compare numbers, not formatted text.
Every function can be called as f(x, ...) or as a method, x.f(...). In the signatures, T and U stand for any type, T[] is a list, ? marks an optional parameter, and (T, number) => boolean is a lambda parameter.
| Function | Description |
|---|---|
round | Rounds half away from zero, optionally to a number of decimal digits. |
floor | Rounds down. |
ceil | Rounds up. |
trunc | Drops the fractional part. |
abs | Absolute value. |
sqrt | Square root. |
clamp | Limits a number to a range. |
toFixed | Formats a number with a fixed number of decimals. |
formatNumber | Formats a number with grouping, e.g. 1,234.5, with optional decimals and locale (default "en-US"; an unsupported locale is an error). |
formatCurrency | Formats an amount in a currency (ISO 4217 code such as "EUR"), with an optional locale (default "en-US"; an unsupported locale is an error). |
min | The smallest value (nulls are skipped); null for an empty list. |
max | The largest value (nulls are skipped); null for an empty list. |
sum | The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is. |
avg | The mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list. |
round
Rounds half away from zero, optionally to a number of decimal digits.
round(number, number?): number
round(2.5) // => 3
round(-2.5) // => -3
round(3.14159, 2) // => 3.14floor
Rounds down.
floor(number): number
floor(2.7) // => 2
floor(-2.1) // => -3ceil
Rounds up.
ceil(number): number
ceil(2.1) // => 3trunc
Drops the fractional part.
trunc(number): number
trunc(-2.7) // => -2abs
Absolute value.
abs(number): number
abs(-4) // => 4sqrt
Square root.
sqrt(number): number
sqrt(16) // => 4
try(sqrt(-1), 0) // => 0clamp
Limits a number to a range.
clamp(number, number, number): number
clamp(150, 0, 100) // => 100
(-5).clamp(0, 100) // => 0toFixed
Formats a number with a fixed number of decimals.
toFixed(number, number): string
toFixed(3.14159, 2) // => "3.14"
(2).toFixed(1) // => "2.0"formatNumber
Formats a number with grouping, e.g. 1,234.5, with optional decimals and locale (default "en-US"; an unsupported locale is an error).
formatNumber(number, number?, string?): string
formatNumber(1234567.891) // => "1,234,567.891"
formatNumber(1234.5, 2) // => "1,234.50"
formatNumber(1234.5, 2, "de-DE") // => "1.234,50"formatCurrency
Formats an amount in a currency (ISO 4217 code such as "EUR"), with an optional locale (default "en-US"; an unsupported locale is an error).
formatCurrency(number, string, string?): string
formatCurrency(1234.5, "EUR") // => "€1,234.50"
formatCurrency(1234.5, "EUR", "de-DE") // => "1.234,50 €"
formatCurrency(99, "JPY") // => "¥99"min
The smallest value (nulls are skipped); null for an empty list.
min(T[]): T | nullmin(number, ...number): number
min(4, 2, 8) // => 2
min([4, 2, 8]) // => 2max
The largest value (nulls are skipped); null for an empty list.
max(T[]): T | nullmax(number, ...number): number
max(4, 2, 8) // => 8
[4, 2, 8].max() // => 8sum
The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.
sum((number | null)[]): number
[1, 2, 3].sum() // => 6
[0.1, 0.2, 0.3].sum() // => 0.6000000000000001avg
The mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list.
avg((number | null)[]): number | null
[2, 4, null].avg() // => 3