Module:FormatNumber
Revision as of 01:22, 26 June 2022 by Naginreed (talk | contribs) (Created page with "local p = {} p.formatNumber = function(number) -- if it was called via {{#invoke}} then number is a frame object and args[1] is the first argument if type(number) == 'table' then number = number.args[1] end local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)') if int == nil then return number end -- reverse the int-string and append a separator to all blocks of 3 digits int = int:reverse():gsub("(%d%d%d)", "%1...")
{{#invoke:FormatNumber|formatNumber|1234567}}
turns 1234567
into 1 234 567
The above documentation is transcluded from Module:FormatNumber/doc. (edit | history)
local p = {}
p.formatNumber = function(number)
-- if it was called via {{#invoke}} then number is a frame object and args[1] is the first argument
if type(number) == 'table' then
number = number.args[1]
end
local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
if int == nil then
return number
end
-- reverse the int-string and append a separator to all blocks of 3 digits
int = int:reverse():gsub("(%d%d%d)", "%1 ")
-- reverse the int-string back remove an optional separator and put the
-- optional minus and fractional part back
return minus .. int:reverse():gsub("^ ", "") .. fraction
end
return p