Module:NumberFormater: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
Line 60: Line 60:
-- If formatnumber returned the same value then return that same value, otherwise format as tooltip.
-- If formatnumber returned the same value then return that same value, otherwise format as tooltip.
if arg1 ~= arg2 then
if arg1 ~= arg2 then
    return string.format(Utility.tooltipstruct, arg1, arg2)
    return string.format(Utility.tooltipstruct, p.formatwithseperator(arg1), arg2)
end
end
return p.formatwithseperator(arg1)
return p.formatwithseperator(arg1)

Revision as of 01:56, 15 March 2024

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 UNIT_EXPONENTS = {
    'K',
    'M',
    'B',
    'T',
    'Q',
    'QQ',
    'S',
    'SP',
    'O',
    'N',
    'D'
}
local function preprocess(numstr)
	-- Strip out anything from the decimal point onwards. This only processes Integers.
	numstr = numstr:gsub("%.(.*)", "")
	-- If it is in e notation, ensure it parses as a string.
	if numstr:find("e") ~= nil then
		numstr = tostring(NumberParser.parse(numstr))
	end
	-- Return if Lots is the word, otherwise return bad number
	if tonumber(numstr) == nil then
		if numstr == "Lots" then
			return numstr 
		end
		return "<BAD NUMBER>"
	end
	return numstr
end

function p.main(frame)
	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)
	-- 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", "")
	
    return string.format("%s %s", n,  UNIT_EXPONENTS[unit])
end


function p.formatnumberwithtootip(arg)
	local arg1 = 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 = ''
	-- 30000
	-- 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
	
	return ret
end

return p