A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.wikipedia.org/wiki/Module:BaseConvert below:

Module:BaseConvert - Wikipedia

From Wikipedia, the free encyclopedia

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

Usage

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

Arguments:

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

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) bra–ket notation ceil, floor calculations :mw:Help:#expr; formatting indicators 3.14, 3.14 (no calculation performed) fraction slant fractions 35 (not for maths/science articles; use standing or upright fractions {{sfrac}} instead) intmath integral symbols
  • langle
  • rangle
  • angbr
  • angular brackets
  • ldelim
  • rdelim
  • multiline delimiters (2–5 lines inclusive) abs absolute values (paired vertical lines) math short text-based formulas mathcal [mathematical] calligraphic font; alternative to LaTeX \mathcal{...} mvar individual italicized maths variables in normal text
  • overline
  • underline
  • a line set above/below a sequence of characters overarc an arc set above a sequence of characters
  • overset
  • underset
  • arbitrary characters/diacritics set above/below one another pars parentheses that can be resized () sfrac "standing" or upright fractions 3/5 (use in maths/science articles instead of{{fraction}})
  • sub
  • sup
  • su
  • subscripts and superscripts tmath Wrap TeX in <math> tags tombstone symbol indicating the end of a proof val measurement values, uncertainties and units vec various overarrows, underarrows, etc.

  • Boxes
  • Tags
  • Notices
  • Boxes Tags
    local p = {}
    
    local digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    local function normalizeFullWidthChars(s)
    	return mw.ustring.gsub(s, '[!-~]', function(s)
    		return mw.ustring.char(mw.ustring.codepoint(s, 1) - 0xFEE0)
    	end)
    end
    
    local function _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)
    	if not from or from < 34 then
    		local c
    		n, c = n:gsub('^(-?)0[Xx]', '%1')
    		if c > 0 and not from then from = 16 end
    	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.
    	local sign = ''
    	local c
    	n, c = n:gsub('^-', '')
    	if c > 0 then sign = '-' 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 = from or 10
    	local num = tonumber(n, from)
    	base = tonumber(base)
    	precision = tonumber(precision)
    	width = tonumber(width)
    
    	if not num or not base then return default or n end
    
    	local i, f = math.modf(num)
    
    	local t = {}
    	repeat
    		local d = (i % base) + 1
    		i = math.floor(i / base)
    		table.insert(t, 1, digits:sub(d, d))
    	until i == 0
    	while #t < (width or 0) do
    		table.insert(t, 1, '0')
    	end
    	local intPart = table.concat(t, '')
    
    	-- compute the fractional part
    	local tf = {}
    	while f > 0 and #tf < (precision or 10) do
    		f = f * base
    		i, f = math.modf(f)
    		table.insert(tf, digits:sub(i + 1, i + 1))
    	end
    
    	-- add trailing zeros if needed
    	if precision and #tf < precision then
    		for i = 1, precision - #tf do
    			table.insert(tf, '0')
    		end
    	end
    
    	local fracPart = table.concat(tf, '')
    
    	-- remove trailing zeros if not needed
    	if not precision then
    		fracPart = fracPart:gsub('0*$', '')
    	end
    
    	-- add the radix point if needed
    	if #fracPart > 0 then
    		fracPart = '.' .. fracPart
    	end
    
    	return (prefix or '') .. sign .. intPart .. fracPart .. (suffix or '')
    end
    
    function p.convert(frame)
    	-- Allow for invocation via #invoke or directly from another module
    	local args
    	if frame == mw.getCurrentFrame() then
    		args = frame.args
    	else
    		args = frame
    	end
    
    	local n = args.n
    	local base = args.base
    	local from = args.from
    	local precision = args.precision
    	local width = args.width
    	local default = args.default
    	local prefix = args.prefix
    	local suffix = args.suffix
    	return _convert(n, base, from, precision, width, default, prefix, suffix)
    end
    
    setmetatable(p, {
    	__index = function(t, k)
    		local from, base = k:match('^([0-9]+)to([0-9]+)$')
    		if not from then return nil end
    		return function(frame)
    			local args = frame.args
    			return _convert(mw.text.trim(args[1]), base, from, args.precision, args.width,
    				args.default, args.prefix, args.suffix)
    		end
    	end
    })
    
    return p
    

    RetroSearch is an open source project built by @garambo | Open a GitHub Issue

    Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

    HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.3