Anda dapat membuat halaman dokumentasi untuk buat pada modul Scribunto ini.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
localp={} localdigits='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' localfunctionnormalizeFullWidthChars(s) returnmw.ustring.gsub(s,'[!-~]',function(s) returnmw.ustring.char(mw.ustring.codepoint(s,1)-0xFEE0) end) end localfunction_convert(n,base,from,precision,width,default,prefix,suffix) n=tostring(n) -- strip off any leading '0x' (unless x is a valid digit in the input base) from=tonumber(from) ifnotfromorfrom<34then localc n,c=n:gsub('^(-?)0[Xx]','%1') ifc>0andnotfromthenfrom=16end end -- check for a negative sign. Do this while the input is still in string form, -- because tonumber doesn't support negative numbers in non-10 bases. localsign='' localc n,c=n:gsub('^-','') ifc>0thensign='-'end -- replace any full-width Unicode characters in the string with their ASCII equivalents n=normalizeFullWidthChars(n) -- handle scientific notation with whitespace around the 'e' e.g. '5 e7' n=n:gsub('%s*[eE]%s*','e') from=fromor10 localnum=tonumber(n,from) base=tonumber(base) precision=tonumber(precision) width=tonumber(width) ifnotnumornotbasethenreturndefaultornend locali,f=math.modf(num) localt={} repeat locald=(i%base)+1 i=math.floor(i/base) table.insert(t,1,digits:sub(d,d)) untili==0 while#t<(widthor0)do table.insert(t,1,'0') end localintPart=table.concat(t,'') -- compute the fractional part localtf={} whilef>0and#tf<(precisionor10)do f=f*base i,f=math.modf(f) table.insert(tf,digits:sub(i+1,i+1)) end -- add trailing zeros if needed ifprecisionand#tf<precisionthen fori=1,precision-#tfdo table.insert(tf,'0') end end localfracPart=table.concat(tf,'') -- remove trailing zeros if not needed ifnotprecisionthen fracPart=fracPart:gsub('0*$','') end -- add the radix point if needed if#fracPart>0then fracPart='.'..fracPart end return(prefixor'')..sign..intPart..fracPart..(suffixor'') end functionp.convert(frame) -- Allow for invocation via #invoke or directly from another module localargs ifframe==mw.getCurrentFrame()then args=frame.args else args=frame end localn=args.n localbase=args.base localfrom=args.from localprecision=args.precision localwidth=args.width localdefault=args.default localprefix=args.prefix localsuffix=args.suffix return_convert(n,base,from,precision,width,default,prefix,suffix) end setmetatable(p,{ __index=function(t,k) localfrom,base=k:match('^([0-9]+)to([0-9]+)$') ifnotfromthenreturnnilend returnfunction(frame) localargs=frame.args return_convert(mw.text.trim(args[1]),base,from,args.precision,args.width, args.default,args.prefix,args.suffix) end end }) returnp