Module:Number: Difference between revisions
From Melvor Idle
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 |
Revision as of 22:04, 12 March 2024
Documentation for this module may be created at Module:Number/doc
--
-- Module that contains functions related to numeric formatting or number manipulation
--
local p = {}
local numberSuffixes = {
k = 1e3,
m = 1e6,
b = 1e9,
t = 1e12,
q = 1e15,
s = 1e18,
o = 1e21,
n = 1e24,
d = 1e30
}
--- Parses a string representation of a number with suffixes
-- @param str (string) The string to parse.
-- @return (number) The numeric value represented by the input string or 0 if the string is NaN.
function p.parseNumber(str)
-- Empty/nil string.
if not str or str == "" then
return 0
end
-- If the input is already a number, return it.
local number = tonumber(str)
if number then
return number
end
local numStr = str:sub(1, -2)
local suffix = str:sub(-1):lower()
if tonumber(numStr) then
local multiplier = numberSuffixes[suffix] or 1
return numStr * multiplier
else
return 0
end
end
--- Format a number with optional suffixes based on its magnitude.
-- @param number (number) The number to be formatted.
-- @param decimals (number) [optional] The number of decimals to include in the formatted output. Default is 2.
-- @return (string) The formatted number as a string
function p.abbreviateNumber(number, decimals)
if type(number) ~= "number" then
error("NaN")
end
-- Round to two decimals by default.
decimals = decimals or 2
for suffix, value in pairs(numberSuffixes) do
if number >= value then
if number % 1 ~= 0 then
return string.format("%." .. decimals .. "f%s", number / value, suffix)
else
return string.format("%.0f%s", number / value, suffix)
end
end
end
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
return p