Module:Format price
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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.