Module:Coins2: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
-- strip commas from input | -- strip commas from input | ||
a = string.gsub(a, ',', '') | a = string.gsub(a, ',', '') | ||
-- check for decimals/division to determine if output should have forced 2 decimal places | -- check for decimals/division to determine if output should have forced 2 decimal places | ||
local has_decimal_or_division = false | local has_decimal_or_division = false | ||
Line 17: | Line 17: | ||
has_decimal_or_division = true | has_decimal_or_division = true | ||
end | end | ||
-- for performance reasons, only calculate expr if required | -- for performance reasons, only calculate expr if required | ||
if tonumber(a) == nil then | if tonumber(a) == nil then | ||
a = tonumber(expr(a)) or 0 | a = tonumber(expr(a)) or 0 | ||
end | end | ||
-- round to two decimal places | -- round to two decimal places if necessary | ||
a = math.floor(a * 100 + 0.5) / 100 | if has_decimal_or_division then | ||
a = math.floor(a * 100 + 0.5) / 100 | |||
else | |||
a = math.floor(a + 0.5) | |||
end | |||
-- format number with commas | -- format number with commas | ||
local formatted_number | |||
if has_decimal_or_division then | if has_decimal_or_division then | ||
local | local integer_part, fractional_part = math.modf(a) | ||
fractional_part = math.abs(fractional_part) | |||
if (integer_part > 999 or integer_part < -999) then | |||
if ( | integer_part = mw.language.getContentLanguage():formatNum(integer_part) | ||
end | |||
if fractional_part == 0 then | |||
formatted_number = tostring(integer_part) | |||
else | |||
fractional_part = string.sub(string.format("%.2f", fractional_part), 2) | |||
formatted_number = integer_part .. fractional_part | |||
end | end | ||
else | else | ||
formatted_number = mw.language.getContentLanguage():formatNum(a) | |||
end | end | ||
return formatted_number | |||
end | end | ||
Latest revision as of 09:28, 19 May 2024
Documentation for this module may be created at Module:Coins2/doc
local p = {}
local function amount(a)
-- convert used globals to locals where possible to improve performance
local math = math
local string = string
local table = table
local mw = mw
local expr = mw.ext.ParserFunctions.expr
-- strip commas from input
a = string.gsub(a, ',', '')
-- check for decimals/division to determine if output should have forced 2 decimal places
local has_decimal_or_division = false
if string.match(a, "[./]") then
has_decimal_or_division = true
end
-- for performance reasons, only calculate expr if required
if tonumber(a) == nil then
a = tonumber(expr(a)) or 0
end
-- round to two decimal places if necessary
if has_decimal_or_division then
a = math.floor(a * 100 + 0.5) / 100
else
a = math.floor(a + 0.5)
end
-- format number with commas
local formatted_number
if has_decimal_or_division then
local integer_part, fractional_part = math.modf(a)
fractional_part = math.abs(fractional_part)
if (integer_part > 999 or integer_part < -999) then
integer_part = mw.language.getContentLanguage():formatNum(integer_part)
end
if fractional_part == 0 then
formatted_number = tostring(integer_part)
else
fractional_part = string.sub(string.format("%.2f", fractional_part), 2)
formatted_number = integer_part .. fractional_part
end
else
formatted_number = mw.language.getContentLanguage():formatNum(a)
end
return formatted_number
end
--
-- {{Coins}} access point
--
function p.amount(frame)
local args = frame:getParent().args
-- for {{coins|111}}
local a = args[1] or '0'
return amount(a)
end
--
-- Module access point
--
function p._amount(a)
a = tostring(a) or '0'
return amount(a)
end
return p