Skip to content

to_number

Builtin · conversion

Parse a number from text, or pass a number through. Analog of std::stod / Luau tonumber.

float to_number(value);
float to_number(value, base);
NameTypeDescription
valueanyText or number.
baseintOptional radix for text (2–36).

float (Luau number). Parse failure → null.

tonumber(value) · tonumber(value, base)

Do not write Luau tonumber in CL++ source. Guard the result if the text might be invalid.

string raw = "42";
float n = to_number(raw);
guard (n != null) else {
warn("not a number");
return;
}
post(n);

Emits:

local raw: string = "42"
local n: number = tonumber(raw)
if not (n ~= nil) then
warn("not a number")
return
end
print(n)

to_string · to_bool · float · int