Module:Number: Difference between revisions

m
Add uniform docs
m (Add clamp function)
m (Add uniform docs)
Line 17: Line 17:
     }
     }
      
      
--Adds commas
--- Formats a number by inserting commas as thousand separators.
-- @param number (number or string) The number to format.
-- @return (string) The formatted number with commas as thousand separators
function p.formatnum(number)
function p.formatnum(number)
if tonumber(number) == nil then
if tonumber(number) == nil then
Line 94: Line 96:
end
end


-- Converts a string to a numeric value, or the default value if the
--- Converts a string to a number or returns a default value if conversion fails.
-- string isn't a number.
-- @param str (string) The string to convert to a number.
-- @param def (number) The default value to return if conversion fails or the input is nil.
-- @return (number) The numeric value represented by the input string or the default value if conversion fails.
function p.toNumberOrDefault(str, def)
function p.toNumberOrDefault(str, def)
local num = tonumber(str)
local num = tonumber(str)
Line 105: Line 109:
end
end


-- Attempts to convert a string to a numeric value.
--- Converts a string to a number or throws an error with a specified message if conversion fails.
-- Throws an error if the value isn't a number.
-- @param str (string) The string to convert to a number.
-- @param errorMsg (string) [optional] The error message to throw if conversion fails. Defaults to "NaN".
-- @return (number) The numeric value represented by the input string.
-- @throws Error with specified error message if conversion fails.
function p.toNumberOrError(str, errorMsg)
function p.toNumberOrError(str, errorMsg)
local num = tonumber(str)
local num = tonumber(str)
914

edits