Skip to main content

tonumber

Function tonumber 

Source
pub fn tonumber(v: any, base: option<number>) -> option<number>
Expand description

When called with no base, tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, returns that number; otherwise, returns nil.

When called with a base, v must be a string to be interpreted as an integer numeral in that base (from 2 to 36, letters Aโ€“Z standing for 10โ€“35).

ยงExamples

print(tonumber("42"))        --> 42
print(tonumber("0x1F"))      --> 31
print(tonumber("3.7e2"))     --> 370.0
print(tonumber("ff", 16))    --> 255
print(tonumber("hello"))     --> nil