<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3ABaseConvert</id>
	<title>Module:BaseConvert - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3ABaseConvert"/>
	<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:BaseConvert&amp;action=history"/>
	<updated>2026-04-04T21:37:11Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://the-democratika.com/wiki/index.php?title=Module:BaseConvert&amp;diff=5974&amp;oldid=prev</id>
		<title>&gt;Johnuniq: update from Module:BaseConvert/sandbox per talk; this removes globals</title>
		<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:BaseConvert&amp;diff=5974&amp;oldid=prev"/>
		<updated>2022-04-13T04:58:12Z</updated>

		<summary type="html">&lt;p&gt;update from &lt;a href=&quot;/wiki/index.php?title=Module:BaseConvert/sandbox&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Module:BaseConvert/sandbox (page does not exist)&quot;&gt;Module:BaseConvert/sandbox&lt;/a&gt; per talk; this removes globals&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local digits = &amp;#039;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&amp;#039;&lt;br /&gt;
&lt;br /&gt;
local function normalizeFullWidthChars(s)&lt;br /&gt;
	return mw.ustring.gsub(s, &amp;#039;[！-～]&amp;#039;, function(s)&lt;br /&gt;
		return mw.ustring.char(mw.ustring.codepoint(s, 1) - 0xFEE0)&lt;br /&gt;
	end)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function _convert(n, base, from, precision, width, default, prefix, suffix)&lt;br /&gt;
	n = tostring(n)&lt;br /&gt;
&lt;br /&gt;
	-- strip off any leading &amp;#039;0x&amp;#039; (unless x is a valid digit in the input base)&lt;br /&gt;
	from = tonumber(from)&lt;br /&gt;
	if not from or from &amp;lt; 34 then&lt;br /&gt;
		local c&lt;br /&gt;
		n, c = n:gsub(&amp;#039;^(-?)0[Xx]&amp;#039;, &amp;#039;%1&amp;#039;)&lt;br /&gt;
		if c &amp;gt; 0 and not from then from = 16 end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- check for a negative sign. Do this while the input is still in string form,&lt;br /&gt;
	-- because tonumber doesn&amp;#039;t support negative numbers in non-10 bases.&lt;br /&gt;
	local sign = &amp;#039;&amp;#039;&lt;br /&gt;
	local c&lt;br /&gt;
	n, c = n:gsub(&amp;#039;^-&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
	if c &amp;gt; 0 then sign = &amp;#039;-&amp;#039; end&lt;br /&gt;
&lt;br /&gt;
	-- replace any full-width Unicode characters in the string with their ASCII equivalents&lt;br /&gt;
	n = normalizeFullWidthChars(n)&lt;br /&gt;
&lt;br /&gt;
	-- handle scientific notation with whitespace around the &amp;#039;e&amp;#039; e.g. &amp;#039;5 e7&amp;#039;&lt;br /&gt;
	n = n:gsub(&amp;#039;%s*[eE]%s*&amp;#039;, &amp;#039;e&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
	from = from or 10&lt;br /&gt;
	local num = tonumber(n, from)&lt;br /&gt;
	base = tonumber(base)&lt;br /&gt;
	precision = tonumber(precision)&lt;br /&gt;
	width = tonumber(width)&lt;br /&gt;
&lt;br /&gt;
	if not num or not base then return default or n end&lt;br /&gt;
&lt;br /&gt;
	local i, f = math.modf(num)&lt;br /&gt;
&lt;br /&gt;
	local t = {}&lt;br /&gt;
	repeat&lt;br /&gt;
		local d = (i % base) + 1&lt;br /&gt;
		i = math.floor(i / base)&lt;br /&gt;
		table.insert(t, 1, digits:sub(d, d))&lt;br /&gt;
	until i == 0&lt;br /&gt;
	while #t &amp;lt; (width or 0) do&lt;br /&gt;
		table.insert(t, 1, &amp;#039;0&amp;#039;)&lt;br /&gt;
	end&lt;br /&gt;
	local intPart = table.concat(t, &amp;#039;&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
	-- compute the fractional part&lt;br /&gt;
	local tf = {}&lt;br /&gt;
	while f &amp;gt; 0 and #tf &amp;lt; (precision or 10) do&lt;br /&gt;
		f = f * base&lt;br /&gt;
		i, f = math.modf(f)&lt;br /&gt;
		table.insert(tf, digits:sub(i + 1, i + 1))&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- add trailing zeros if needed&lt;br /&gt;
	if precision and #tf &amp;lt; precision then&lt;br /&gt;
		for i = 1, precision - #tf do&lt;br /&gt;
			table.insert(tf, &amp;#039;0&amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local fracPart = table.concat(tf, &amp;#039;&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
	-- remove trailing zeros if not needed&lt;br /&gt;
	if not precision then&lt;br /&gt;
		fracPart = fracPart:gsub(&amp;#039;0*$&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- add the radix point if needed&lt;br /&gt;
	if #fracPart &amp;gt; 0 then&lt;br /&gt;
		fracPart = &amp;#039;.&amp;#039; .. fracPart&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return (prefix or &amp;#039;&amp;#039;) .. sign .. intPart .. fracPart .. (suffix or &amp;#039;&amp;#039;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.convert(frame)&lt;br /&gt;
	-- Allow for invocation via #invoke or directly from another module&lt;br /&gt;
	local args&lt;br /&gt;
	if frame == mw.getCurrentFrame() then&lt;br /&gt;
		args = frame.args&lt;br /&gt;
	else&lt;br /&gt;
		args = frame&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local n = args.n&lt;br /&gt;
	local base = args.base&lt;br /&gt;
	local from = args.from&lt;br /&gt;
	local precision = args.precision&lt;br /&gt;
	local width = args.width&lt;br /&gt;
	local default = args.default&lt;br /&gt;
	local prefix = args.prefix&lt;br /&gt;
	local suffix = args.suffix&lt;br /&gt;
	return _convert(n, base, from, precision, width, default, prefix, suffix)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
setmetatable(p, {&lt;br /&gt;
	__index = function(t, k)&lt;br /&gt;
		local from, base = k:match(&amp;#039;^([0-9]+)to([0-9]+)$&amp;#039;)&lt;br /&gt;
		if not from then return nil end&lt;br /&gt;
		return function(frame)&lt;br /&gt;
			local args = frame.args&lt;br /&gt;
			return _convert(mw.text.trim(args[1]), base, from, args.precision, args.width,&lt;br /&gt;
				args.default, args.prefix, args.suffix)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>&gt;Johnuniq</name></author>
	</entry>
</feed>