Jump to content
Wikipedia The Free Encyclopedia

Module:BaseConvert

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 37,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.

Converts numbers to a specified base between 2 and 36, for use in templates such as {{Binary }}, {{Octal }}, {{Hexadecimal }}, etc.

Usage

localBaseConvert=require('Module:BaseConvert')
BaseConvert.convert({n=14600926,base=16})-- returns 'DECADE'

Arguments:

  • n - (required) the number to be converted, as a string. It may be a number instead, if the input base is 10.
  • base - (required) the base to which the number should be converted. May be between 2 and 36, inclusive.
  • from - the base of the input. Defaults to 10 (or 16 if the input has a leading '0x'). Note that bases other than 10 are not supported if the input has a fractional part.
  • precision - number of digits to be rendered after the radix point. Trailing zeros will be added if needed. If not specified, however many digits are needed will be shown, up to 10.
  • width - minimum number of digits to be rendered before the radix point. Leading zeros will be added if needed.
  • default - Value to return if n is empty or non-numeric. Defaults to the value of n.
  • prefix / suffix - wikitext to add before/after the returned result. Will not be added if n is empty or non-numeric. For example, you might use a prefix of 0x when converting to hex, or a suffix of <sub>8</sub> when converting to octal.

From templates

In wikimarkup, this module may be called with a function name ntom, e.g.:

Markup Renders as
{{#invoke:BaseConvert|16to10| FF }}

255

{{#invoke:BaseConvert|10to36|500}}

DW

{{#invoke:BaseConvert|10to16|Foo|default=0}}

0

All options above are supported, excluding |base=, |from= and |n= which are set by the mandatory options.

Edge cases

Markup Renders as
{{#invoke:BaseConvert|10to10|500}}

500

{{#invoke:BaseConvert|10to10|FooBar}}

FooBar

{{#invoke:BaseConvert|10to10|FooBar|default=}}
{{#invoke:BaseConvert|10to16|Foo}}

Foo

Math templates
  • Functions
  • Numeral systems
Functions
Numeral systems
Conversions
convert many units (see: list)
cvt abbreviated {{convert }}
convinfobox {{convert }} for infoboxes
bbl to t barrels of oil to tonnes
long ton long hundredweights, quarters and pounds to kilograms;
long tons and hundredweights to pounds and metric tons
miles-chains miles and chains to kilometres linking "chains"
decdeg degrees, minutes, and seconds to decimal degrees
deg2dms decimal degrees to degrees, minutes, and seconds
deg2hms decimal degrees to hour angle (in hours, minutes, and seconds)
hms2deg hour angle (in hours, minutes, and seconds) to decimal degrees
inflation calculate inflation of Consumer Price Index-related prices
pop density population density in an area
track gauge railway track gauges
Notation and formatting
bigmath bigger font to match TeX \displaystyle (standalone formulas only) math short text-based formulas
mathcal [mathematical] calligraphic font; alternative to LaTeX \mathcal{...} tombstone symbol indicating the end of a proof
mvar individual italicized maths variables in normal text val measurement values, uncertainties and units
a line set above/below a sequence of characters vec various overarrows, underarrows, etc.
abs absolute values (paired vertical lines)
  • langle
  • rangle
  • angbr
  • angular brackets
  • bra-ket
  • braket
  • bra
  • ket
  • bra–ket notation
  • ldelim
  • rdelim
  • multiline delimiters (2–5 lines inclusive)
    ceil, floor calculations :mw:Help:#expr; formatting indicators 3.14, 3.14 (no calculation performed) pars parentheses that can be resized ()
    fraction slant fractions 35 (not for maths/science articles; use standing or upright fractions {{sfrac}} instead) sfrac "standing" or upright fractions 3/5 (use in maths/science articles instead of{{fraction}})
    intmath integral symbols
  • sub
  • sup
  • su
  • subscripts and superscripts
  • overset
  • underset
  • arbitrary characters/diacritics set above/below one another    
    tmath Wrap TeX in <math> tags
  • Boxes
  • Tags
  • Notices
  • The above documentation is transcluded from Module:BaseConvert/doc. (edit | history)
    Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
    Subpages of this module.

     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
    

    AltStyle によって変換されたページ (->オリジナル) /