Jump to content

Module:Spellnum per MOS

Permanently protected module
From Wikipedia, the free encyclopedia
(Redirected from Module:ConvertNumericPerMOS)

localp={}localwords={"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, usfunctionp.spellnum(args)localframe=mw.getCurrentFrame()localnumeral=tonumber((string.gsub(args[1],",","")))localpass_zero="zero"ifargs['zero']~=nilandargs['zero']~=''thenpass_zero=args['zero']end-- Always return numerals for negative numbers, non-integers, and if (forcenum and numeral >= 10).ifnumeral<0ormath.fmod(numeral,1)~=0or(numeral>=10andframe:expandTemplate{title='yesno',args={args['forcenum']}}=='yes')thenreturnmw.language.getContentLanguage():formatNum(numeral)end-- Convert numeral to wordslocalspelled=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 wordsifmw.ustring.find(spelled,'%a+[ %-]%a+[ %-]%a+')then-- Handle numbers larger than one millionifnumeral>=1000000andnumeral<=1000000000000000thenlocalsize=math.min(4,math.floor(math.log10(numeral)/3))numeral=numeral/1000^sizereturn({"%.1f ","%d ","%d "})[1+math.floor(math.log10(numeral))]:format(numeral)..words[size]endreturnmw.language.getContentLanguage():formatNum(numeral)elsereturnspelledendendfunctionp.main(frame)returnp.spellnum(frame.args)endreturnp
close