number
The number function and its methods handle numeric conversion and operations.
Conversion
number(logical)
Convert boolean to number.
number(true) // 1
number(false) // 0
number(text, radix)
Parse text to number. Radix is 2-36 (default: 10).
number("42") // 42
number("ff", 16) // 255
number("1010", 2) // 10
number(text, format)
Parse formatted numbers.
| Format | Description |
|---|---|
"" |
Standard decimal |
"u" |
Underbar separator (1_000) |
"d" |
Comma separator (1,000) |
"s" |
Space separator (1 000) |
"v" |
European (1.000,50) |
"b" |
Binary |
"o" |
Octal |
"h" |
Hexadecimal |
"j" |
JavaScript style (0x, 0o, 0b prefixes) |
number("1,000", "d") // 1000
number("0xff", "j") // 255
Methods
abs(n)
Absolute value.
abs(-5) // 5
abs(5) // 5
sign(n)
Returns -1, 0, or 1.
sign(-5) // -1
sign(0) // 0
sign(5) // 1
floor(n, place)
Round down.
floor(4.9) // 4
floor(4.567, 2) // 4.56
ceiling(n, place)
Round up.
ceiling(4.1) // 5
ceiling(4.123, 2) // 4.13
round(n, place)
Round to nearest.
round(4.5) // 5
round(4.567, 2) // 4.57
trunc(n, place)
Truncate toward zero.
trunc(4.9) // 4
trunc(-4.9) // -4
whole(n)
Get the integer part.
whole(4.9) // 4
whole(-4.9) // -4
fraction(n)
Get the fractional part.
fraction(4.75) // 0.75
min(...values)
Return the smallest value.
min(3, 1, 4, 1, 5) // 1
max(...values)
Return the largest value.
max(3, 1, 4, 1, 5) // 5
remainder(dividend, divisor)
Compute remainder.
remainder(17, 5) // 2