Module:NumberFormater

From IdleOn MMO Wiki
Abbreviation Name Value Equivalent
K Thousand or Kilodillion 10^3
M Million 10^6
B Billion 10^9
T Trillion 10^12
Qa Quadrillion 10^15
Qi Quintillion 10^18
Sx Sextillion 10^21
Sp Septillion 10^24
O Octillion 10^27
No Nonillion 10^30
D Decillion 10^33
Udc Undecillion 10^36
Dd Duodecillion 10^39
Tdc Tredecillion 10^42
Qt Quattuordecillion 10^45
Qd Quindecillion 10^48
Sd Sexdecillion 10^51
St Septendecillion 10^54
Od Octodecillion 10^57
Nm Novemdecillion 10^60
Vg Vigintillion 10^63
Uvg Unvigintillion 10^66
Dvg Duovigintillion 10^69
Tvg Tresvigintillion or Trevigintillion 10^72
Qav Quattuorvigintillion 10^75
Qvg Quinvigintillion 10^78
Svg Sexvigintillion or Sesvigintillion 10^81
Spv Septemvigintillion or Septenvigintillion 10^84
Ovg Octovigintillion 10^87
Nvg Novemvigintillion or Novenvigintillion 10^90
Tg Trigintillion 10^93
Ut Untrigintillion 10^96
Dt Duotrigintillion 10^99
Reamining will follow e# notation.

* Sources from an external Github site.


local p = {}
local NumberParser = require('Module:NumberParser')
local Utility = require("Module:Utility")
local decimals = ''
local UNIT_EXPONENTS = {
	-- Currently supporting up to ^93
    'K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'O', 
    'No', 'D', 'Udc', 'Dd', 'Tdc', 'Qt', 'Qd', 'Sd', 
    'St', 'Od', 'Nm', 'Vg', 'Uvg', 'Dvg', 'Tvg', 'Qav', 
    'Qvg', 'Svg', 'Spv', 'Ovg', 'Nvg', 'Tg', 'Ut', 'Dt'
}
local function preprocess(inNum)
	local numstr = tostring(inNum)
	-- If it is in e notation, ensure it parses as a string.
	if numstr:lower():find("e") ~= nil then
		numstr = NumberParser.parse(numstr)
	end
	-- Strip out anything from the decimal point onwards.
	decimals = numstr:find("%.(.*)") ~= nil and numstr:sub(numstr:find("%.(.*)"))  or ""
	numstr = numstr:gsub("%.(.*)", "")
	-- check if decimal is extrenious
	if decimals ~= nil and tonumber(decimals:sub(2)) == 0 then decimals = '' end
	-- Return if Lots is the word, otherwise return bad number
	if tonumber(numstr) == nil then
		-- This could potentially break things if Lots is returned to another process.
		if numstr == "Lots" then
			return numstr 
		end
		return "<BAD NUMBER>[[Category:Error Template NumberFormater]][[Category:Error Template NumberFormater bad number]]"
	end
	return numstr
end

function p.main(frame)
	-- If the first argument is nil or blank, return nil.
	if frame.args[1] == nil or frame.args[1] == "" then
		return "<NIL>[[Category:Error Template NumberFormater]][[Category:Error Template NumberFormater nil]]"
	end
	local numstr = preprocess(frame.args[1])
	if frame.args.formattype ~= "tooltip" then
		return p.formatnumber(numstr)
	else
		return p.formatnumberwithtootip(numstr)
	end
end

function p.formatnumber(arg)
	if tonumber(arg) == nil then return arg end
	-- how many digts are after the first digit divided by 3.
	local unit = math.floor((arg:len() - 1) / 3)
	if unit == 0 or arg:len() <= 5 then
		return arg
	end
	
	local n = string.format("%.1f", tonumber(arg) / math.pow(10, unit*3)):gsub("%.0", "")
	if unit <= 33 then
		return string.format("%s %s", n,  UNIT_EXPONENTS[unit])
	else
		return string.format("%s e<sup>%s</sup>", n,  unit*3)
	end
end


function p.formatnumberwithtootip(arg)
	local arg1 = preprocess(arg)
	local arg2 = p.formatnumber(arg1)
	
	-- If formatnumber returned the same value then return that same value, otherwise format as tooltip.
	if arg1 ~= arg2 then
	    return string.format(Utility.tooltipstruct, p.formatwithseperator(arg1), arg2)
	end
	return p.formatwithseperator(arg1)
end

function p.formatwithseperator(arg)
	local ret = ''
	arg = preprocess(arg)
	-- Iterate from the end and add in commas.
	while arg:len() > 3 do
		ret = "," .. arg:sub(-3) .. ret
		arg = arg:sub(1, -4)
	end
	-- Prepend remaining digits.
	ret = arg .. ret .. decimals
	-- clear decimals
	decimals = ''
	
	return ret
end

return p