Module:Gapnum
- Аԥсшәа
- العربية
- অসমীয়া
- تۆرکجه
- Basa Bali
- বাংলা
- Беларуская (тарашкевіца)
- भोजपुरी
- Буряад
- Català
- Чӑвашла
- Čeština
- Dansk
- الدارجة
- Deutsch
- Эрзянь
- Español
- فارسی
- Føroyskt
- Gaeilge
- Galego
- 한국어
- Hausa
- Հայերեն
- हिन्दी
- Ilokano
- Bahasa Indonesia
- Italiano
- עברית
- Jawa
- Latina
- Lietuvių
- Ligure
- Македонски
- മലയാളം
- ဘာသာမန်
- 閩東語 / Mìng-dĕ̤ng-ngṳ̄
- Монгол
- မြန်မာဘာသာ
- Na Vosa Vakaviti
- 日本語
- Oʻzbekcha / ўзбекча
- ပအိုဝ်ႏဘာႏသာႏ
- Português
- Română
- Scots
- Simple English
- Slovenščina
- کوردی
- Српски / srpski
- Suomi
- Svenska
- தமிழ்
- Taqbaylit
- တႆး
- Tetun
- ไทย
- Türkçe
- Українська
- اردو
- Vèneto
- Tiếng Việt
- 粵語
- 中文
- Kadazandusun
- Kumoring
Appearance
From Wikipedia, the free encyclopedia
This module depends on the following other modules:
Warning This Lua module is used on approximately 43,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module is used by {{val }}.
Use in other modules
gaps
The gaps function can be useful for formatting in other modules that work with displaying large numbers.
localgaps=require('Module:Gapnum').gaps
Using the gaps function, the first argument is the number to format. The second argument can be a table with keys that tell the module how to format. The table keys that can be used are:
gap- a number with CSS units (px, em, en, etc) that define the size of the gap between numbers. If blank, the module will use0.25em.prec- a number that determines the precision of the decimal part of the number. If the precision is less than the number of digits, extra digits will be removed without rounding; if it is more, zeroes will be added to the end to create the desired precision. If blank, the module will use-1, which means the precision will be the same as the number given; no digits added or removed.
Note that the return statement is a table. This means more styling or text can be added to the wrapper span tag, but it may also mean that tostring() may be required when used in other modules.
localgaps=require('Module:Gapnum').gaps functionexample() localn=123456.78900011 -- Example for just simple formatting of a number -- n_gaps will use the default, .25em gaps and no change in precision -- The result will have its gaps created with inline css -- But the result would look like: -- 123 456.789 000 11 localn_gaps=gaps(n) -- Different gap size -- These will format n into the same groups as above -- But the spaces between the groups will be larger and smaller, respectively localn_big_gaps=gaps(n,{gap='1em'}) localn_small_gaps=gaps(n,{gap='1px'}) -- Different precision -- n_prec_5 will use the number 123456.78900 -- The result would look like: -- 123 456.789 00 localn_prec_5=gaps(n,{prec=5}) -- n_prec_10 will use the number 123456.7890001100 -- The result would look like: -- 123 456.789 000 1100 localn_prec_10=gaps(n,{prec=10}) -- Both different gaps and precision can be used: localn_big_5=gaps(n,{gap='1em',prec=5}) localn_small_10=gaps(n,{gap='1px',prec=10}) end
groups
The groups function can be used in other modules to separate a number into groups as gaps does, but instead of a formatted string, the function will return tables whose elements are the separated groups.
localgroups=require('Module:Gapnum').groups functionexample() -- This will return one table: -- {123,456} localn1=groups(123456) -- This will return two tables, each assigned to a different variable: -- n2a will be: -- {1,234} -- n2b will be: -- {567,89} localn2a,n2b=groups(1234.56789) -- This will return two tables: -- An integer part is always returned, even if it is 0 -- n3a will be: -- {0} -- n3b will be: -- {123,4567} localn3a,n3b=groups(0.1234567) -- Just like the other functions, a precision can be defined -- precision is simply the second parameter -- n4a will be: -- {123} -- n4b will be: -- {456,700,00} localn4a,n4b=groups(123.4567,8) end
The above documentation is transcluded from Module:Gapnum/doc. (edit | history)
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Subpages of this module.
localp={} localgetArgs functionp.main(frame) ifnotgetArgsthen getArgs=require('Module:Arguments').getArgs end localargs=getArgs(frame,{wrappers='Template:Gapnum'}) localn=args[1] ifnotnthen error('Parameter 1 is required') elseifnottonumber(n)andnottonumber(n,36)then-- Validates any number with base ≤ 36 error('Unable to convert "'..args[1]..'" to a number') end localgap=args.gap localprecision=tonumber(args.prec) returnp.gaps(n,{gap=gap,prec=precision}) end -- Not named p._main so that it has a better function name when required by Module:Val functionp.gaps(n,tbl) localnstr=tostring(n) ifnottblthen tbl={} end localgap=tbl.gapor'.25em' localint_part,frac_part=p.groups(n,tbl.prec) localret=mw.html.create('span') :css('white-space','nowrap') -- No gap necessary on first group :wikitext(table.remove(int_part,1)) -- Build int part for_,vinipairs(int_part)do ret:tag('span') :css('margin-left',gap) :wikitext(v) end iffrac_partthen -- The first group after the decimal shouldn't have a gap ret:wikitext('.'..table.remove(frac_part,1)) -- Build frac part for_,vinipairs(frac_part)do ret:tag('span') :css('margin-left',gap) :wikitext(v) end end returnret end -- Creates tables where each element is a different group of the number functionp.groups(num,precision) localnstr=tostring(num) ifnotprecisionthen precision=-1 end localdecimalloc=nstr:find('.',1,true) localint_part,frac_part ifdecimalloc==nilthen int_part=nstr else int_part=nstr:sub(1,decimalloc-1) frac_part=nstr:sub(decimalloc+1) end -- only define ret_i as an empty table, let ret_d stay nil localret_i,ret_d={} -- Loop to handle most of the groupings; from right to left, so that if a group has less than 3 members, it will be the first group whileint_part:len()>3do -- Insert in first spot, since we're moving backwards table.insert(ret_i,1,int_part:sub(-3)) int_part=int_part:sub(1,-4) end -- handle any left over numbers ifint_part:len()>0then table.insert(ret_i,1,int_part) end ifprecision~=0andfrac_partthen ret_d={} ifprecision==-1then precision=frac_part:len() end -- Reduce the length of the string if required precision is less than actual precision -- OR -- Increase it (by adding 0s) if the required precision is more than actual localoffset=precision-frac_part:len() ifoffset<0then frac_part=frac_part:sub(1,precision) elseifoffset>0then frac_part=frac_part..string.rep('0',offset) end -- Allow groups of 3 or 2 (3 first) forvinstring.gmatch(frac_part,'%d%d%d?')do table.insert(ret_d,v) end -- Preference for groups of 4 instead of groups of 1 at the end if#frac_part%3==1then iffrac_part:len()==1then ret_d={frac_part} else locallast_g=ret_d[#ret_d]or'' last_g=last_g..frac_part:sub(-1) ret_d[#ret_d]=last_g end end end returnret_i,ret_d end returnp