Skip to content

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.

FunctionDescription
roundRounds half away from zero, optionally to a number of decimal digits.
floorRounds down.
ceilRounds up.
truncDrops the fractional part.
absAbsolute value.
sqrtSquare root.
clampLimits a number to a range.
toFixedFormats a number with a fixed number of decimals.
formatNumberFormats a number with grouping, e.g. 1,234.5, with optional decimals and locale (default "en-US"; an unsupported locale is an error).
formatCurrencyFormats an amount in a currency (ISO 4217 code such as "EUR"), with an optional locale (default "en-US"; an unsupported locale is an error).
minThe smallest value (nulls are skipped); null for an empty list.
maxThe largest value (nulls are skipped); null for an empty list.
sumThe sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.
avgThe 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
bonsai
round(2.5) // => 3
round(-2.5) // => -3
round(3.14159, 2) // => 3.14

floor ​

Rounds down.

  • floor(number): number
bonsai
floor(2.7) // => 2
floor(-2.1) // => -3

ceil ​

Rounds up.

  • ceil(number): number
bonsai
ceil(2.1) // => 3

trunc ​

Drops the fractional part.

  • trunc(number): number
bonsai
trunc(-2.7) // => -2

abs ​

Absolute value.

  • abs(number): number
bonsai
abs(-4) // => 4

sqrt ​

Square root.

  • sqrt(number): number
bonsai
sqrt(16) // => 4
try(sqrt(-1), 0) // => 0

clamp ​

Limits a number to a range.

  • clamp(number, number, number): number
bonsai
clamp(150, 0, 100) // => 100
(-5).clamp(0, 100) // => 0

toFixed ​

Formats a number with a fixed number of decimals.

  • toFixed(number, number): string
bonsai
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
bonsai
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
bonsai
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 | null
  • min(number, ...number): number
bonsai
min(4, 2, 8) // => 2
min([4, 2, 8]) // => 2

max ​

The largest value (nulls are skipped); null for an empty list.

  • max(T[]): T | null
  • max(number, ...number): number
bonsai
max(4, 2, 8) // => 8
[4, 2, 8].max() // => 8

sum ​

The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.

  • sum((number | null)[]): number
bonsai
[1, 2, 3].sum() // => 6
[0.1, 0.2, 0.3].sum() // => 0.6000000000000001

avg ​

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
bonsai
[2, 4, null].avg() // => 3