Module:Spellnum per MOS
{{Module rating }}
Usage
Implements Template:Spellnum per MOS.
{{#invoke:Spellnum per MOS|main|number to format|if second arg equals 1, prefer numerals}}
For use by other Lua modules:
local spellnum = require('Module:Spellnum per MOS').spellnum
spellnum{ 11 } -- returns 'eleven'
local p = {}
local words = {"thousand", "million", "billion", "trillion"} -- We don't need to go higher than this, no-one knows what an octillion is.
-- For use by other scripts. Takes arguments:
-- - 1: string or number, value to convert
-- - forcenum: string for Template:Yesno, forces a result in digits for all n ≥ 10.
-- - formating options for spellnum: zero, adj, ord, us
function p.spellnum(args)
local frame = mw.getCurrentFrame()
local numeral = tonumber((string.gsub(args[1], ",", "")))
if not numeral then
return args[1]
end
local pass_zero = "zero"
if args['zero'] ~= nil and args['zero'] ~= '' then
pass_zero = args['zero']
end
-- Always return numerals for negative numbers, non-integers, and if (forcenum and numeral >= 10).
if numeral < 0 or
math.fmod(numeral, 1) ~= 0 or
(numeral >= 10 and frame:expandTemplate{ title = 'yesno', args = {args['forcenum']} } == 'yes') then
return mw.language.getContentLanguage():formatNum(numeral)
end
-- Convert numeral to words
local spelled = frame:expandTemplate{ title = 'spellnum', args = {
numeral, zero = pass_zero, adj = args['adj'], ord = args['ord'], us = args['us']}}
-- Return numerals if more than two words would be needed, else return words
if mw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+') then
-- Handle numbers larger than one million
if numeral >= 1000000 and numeral <= 1000000000000000 then
local size = math.min(4, math.floor(math.log10(numeral) / 3))
numeral = numeral / 1000^size
return ({"%.1f ", "%d ", "%d "})[1 + math.floor(math.log10(numeral))]:format(numeral) .. words[size]
end
return mw.language.getContentLanguage():formatNum(numeral)
else
return spelled
end
end
function p.main(frame)
return p.spellnum(frame.args)
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.