Anonymous

Module:Number: Difference between revisions

From Melvor Idle
m
Add functions from Module:Shared
m (Add functions for numeric parsing with suffixes (k, m, b, etc))
m (Add functions from Module:Shared)
Line 65: Line 65:


     return tostring(number)
     return tostring(number)
end
-- Converts a string to a numeric value, or the default value if the
-- string isn't a number.
function p.toNumberOrDefault(str, def)
local num = tonumber(str)
if num then
return num
else
return def
end
end
-- Attempts to convert a string to a numeric value.
-- Throws an error if the value isn't a number.
function p.toNumberOrError(str, errorMsg)
local num = tonumber(str)
local msg = errorMsg
if msg == nil then
msg = "NaN"
end
if num then
return num
else
error(msg)
end
end
end


return p
return p
2,875

edits