tonum( str )
Converts a string representation of a decimal, hexadecimal, or binary number to a number value.
The string.

Returns the number, or no value (effectively nil) if the string does not represent a number.

The tonum() function accepts string representations of all numbers within the PICO-8 number range (-32768.0 to 32767.0). If the string represents a number outside of this range, it returns -32768.

(As of PICO-8 v0.1.11d, tonum('xyz') reports that the string can't be parsed as a number by returning "no value." In Lua, this is technically distinct from returning nil, though it is coerced to nil when used in an assignment or comparison.)

Examples Edit

x = tonum('12345')      -- 12345
x = tonum('-12345.67')  -- -12345.67
x = tonum('0x0f')       -- 15
x = tonum('')   -- 15.6709
x = tonum('0b1001')     -- 9
x = tonum('32767')      -- 32767
x = tonum('99999')      -- -32768
x = tonum('xyz')        -- nil

See also Edit

Community content is available under CC-BY-SA unless otherwise noted.