Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Format price
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
local p = {} local names = { "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion" } -- two_decimals: boolean, whether to forbid a 1 d.p. result, so 2 becomes "2", -- 2.1 "2.10", and 2.123 "2.123" function p.decimal(val, length, two_decimals) local precision = math.max(0, math.ceil(length - 1 - math.log10(val))) if two_decimals and precision == 1 then precision = 2 end local result = ("%%.%uf"):format(precision):format(val) if two_decimals then result = result:gsub('%.0*$', ''):gsub('(%.[0-9][0-9][0-9]-)0+$', '%1') else result = result:gsub('%.?0*$', '') end result = mw.text.split(result, '%.') result[1] = result[1]:reverse():gsub('[0-9][0-9][0-9]', '%0,'):reverse():gsub('^,','') return table.concat(result, '.') end function p.format(val, precision) local scale = math.floor(math.log10(val) / 3) - 1 if scale > 0 then scale = math.min(scale, #names) val = val / math.pow(10, 3 * (1 + scale)) return p.decimal(val, 3) .. '\194\160' .. names[scale] end if precision <= 0 then val = math.floor(val / math.pow(10, -precision) + 0.5) val = val * math.pow(10, -precision) end return p.decimal(val, precision + 1, true) end p[''] = function(frame) local val = tonumber(frame.args[1]) if not val or val < 0 then error('[[Module:Format price|Format price]] error: cannot parse value "' .. (frame.args[1] or '') .. '"') end return p.format(val, tonumber(frame.args[2]) or 2) end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Format price/doc
(
edit
)