Conversions API

Functions for converting between types.

num(value)

num(value)

Converts a string to a number. Leading and trailing whitespace is trimmed before parsing.

Returns: number

Errors: if value is not a string, is empty, or is not a valid number format.

num("42")        // 42
num("3.14")      // 3.14
num("  -7  ")    // -7
num("1e10")      // 10000000000

str(value)

str(value)

Converts any value to its string representation.

str(42)          // "42"
str(3.14)        // "3.14"
str(true)        // "true"
str(null)        // "null"
str([1, 2, 3])   // "[1, 2, 3]"

str(format, ...)

str(format, ...)

Formats a string using a format template and arguments. Supports up to 25 arguments after the format string.

SpecifierTypeDescription
%sstringInsert a string value
%nnumberInsert a number
%bbooleanInsert true or false
%vanyInsert any value’s string representation
%%Literal % character
str("Hello, %s!", "world")              // "Hello, world!"
str("%s is %n years old", "Alice", 30)  // "Alice is 30 years old"
str("Result: %b", true)                 // "Result: true"
str("Value: %v", [1, 2])               // "Value: [1, 2]"
str("100%%")                            // "100%"

See also: String APIMath API