#[test]
fn test() {
let a = 100.toString();
let b = Number.toString(100);
assertEq(a, b);
assertEq(a, "100");
}
Number.or(val: int | float | units, ...):unknown
Returns the first non-empty (null or void) argument, just like the "or" function.
#[test]
fn test() {
let val = 5;
assertEq(val.or(2), 5);
val = null;
assertEq(val.or(3), 3);
}
Parse Strings to Numbers
Number.parse(val: str):int | float | units
Parse a string into a number using the Stof parser. This includes integers, floating-point numbers, hexadecimal, octal, and binary values. It also includes the readability characters Stof supports: "+" prefix, "-" prefix (of course), and ignores "_" characters. This function also parses Stof units.
fn example() {
let val = Number.parse("+1.24_45e4"); // 12445
let hex = Number.parse("0x35_Fa_60"); // must use 0x
let oct = Number.parse("0o55"); // must use 0o
let bin = Number.parse("0b11"); // must use 0b
let units = Number.parse("12kg"); // units of kilograms
}
Number.parseHex(val: str):int
Works exactly like Number.parse(...), however, if the value does not start with "0x", this function will add it as a prefix before parsing.
Number.parseOct(val: str):int
Works exactly like Number.parse(...), however, if the value does not start with "0o", this function will add it as a prefix before parsing.
Number.parseBin(val: str):int
Works exactly like Number.parse(...), however, if the value does not start with "0b", this function will add it as a prefix before parsing.
Number.toHexString(val: int | float | units):str
Converts the integer portion of this value to a hexadecimal string. The resulting string is always capital hex characters (A-F) and does not start with "0x".
Number.round(val: int | float | units, places?: int):float
Round this val to the nearest integer. If value is half-way between two integers, rounds away from 0.0. If given a number of places to round to, move the decimal right that many places, round, then moves it back.
Computes the arctangent of a number. Return value is in radians in the range -pi/2, pi/2.
#[test]
fn test() {
assertEq(0.atan(), 0deg);
}
Number.atan2(val: int | float | units, other: float):rad
Computes the four quadrant arctangent of "val" and "other" in radians.
x = 0, y = 0: 0
x >= 0: arctan(y/x) -> [-pi/2, pi/2]
y >= 0: arctan(y/x) + pi -> (pi/2, pi]
y < 0: arctan(y/x) - pi -> (-pi, -pi/2)
Number.sinh(val: int | float | units):float
Hyperbolic sine function.
Number.cosh(val: int | float | units):float
Hyperbolic cosine function.
Number.tanh(val: int | float | units):float
Hyperbolic tangent function.
Number.asinh(val: int | float | units):float
Inverse hyperbolic sine function.
Number.acosh(val: int | float | units):float
Inverse hyperbolic cosine function.
Number.atanh(val: int | float | units):float
Inverse hyperbolic tangent function.
Iteration
Number.len(val: int | float | units):int
Returns the integer part of "val". Non-integer values are truncated towards zero.
This function is provided to support iteration of numbers.
Number.at(val: int | float | units, index: int):int
Returns the integer part of "index" if index is less than the integer part of "val", otherwise, returns the integer part of "val". Non-integer values for both "val" and "index" are truncated towards zero.
#[test]
fn test() {
// For loops use "len" to get the length and "at" to get each value
for (i in 10) {
pln(i); // will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
}
Units
Number.units(val: units):str | null
Returns the units of this number or null if no units are defined.