Module:NumberFormater: Difference between revisions
From IdleOn MMO Wiki
Kiokurashi (talk | contribs) mNo edit summary |
Kiokurashi (talk | contribs) mNo edit summary |
||
Line 2: | Line 2: | ||
local NumberParser = require('Module:NumberParser') | local NumberParser = require('Module:NumberParser') | ||
local Utility = require("Module:Utility") | local Utility = require("Module:Utility") | ||
local decimals = '' | |||
local UNIT_EXPONENTS = { | local UNIT_EXPONENTS = { | ||
'K', | 'K', | ||
Line 16: | Line 17: | ||
} | } | ||
local function preprocess(numstr) | local function preprocess(numstr) | ||
-- Strip out anything from the decimal point onwards. | -- Strip out anything from the decimal point onwards. | ||
decimals = numstr:find("%.(.*)") ~= nil and numstr:sub(numstr:find("%.(.*)")) or "" | |||
numstr = numstr:gsub("%.(.*)", "") | numstr = numstr:gsub("%.(.*)", "") | ||
-- check if decimal is extrenious | |||
if decimals ~= nil and tonumber(decimals:sub(2)) == 0 then decimals = '' end | |||
-- If it is in e notation, ensure it parses as a string. | -- If it is in e notation, ensure it parses as a string. | ||
if numstr:find("e") ~= nil then | if numstr:find("e") ~= nil then | ||
Line 24: | Line 28: | ||
-- Return if Lots is the word, otherwise return bad number | -- Return if Lots is the word, otherwise return bad number | ||
if tonumber(numstr) == nil then | if tonumber(numstr) == nil then | ||
-- This could potentially break things if Lots is returned to another process. | |||
if numstr == "Lots" then | if numstr == "Lots" then | ||
return numstr | return numstr | ||
Line 55: | Line 60: | ||
function p.formatnumberwithtootip(arg) | function p.formatnumberwithtootip(arg) | ||
local arg1 = | local arg1 = preprocess(arg) | ||
local arg2 = p.formatnumber(arg1) | local arg2 = p.formatnumber(arg1) | ||
Line 67: | Line 72: | ||
function p.formatwithseperator(arg) | function p.formatwithseperator(arg) | ||
local ret = '' | local ret = '' | ||
arg = preprocess(arg) | |||
-- Iterate from the end and add in commas. | -- Iterate from the end and add in commas. | ||
while arg:len() > 3 do | while arg:len() > 3 do | ||
Line 77: | Line 80: | ||
-- Prepend remaining digits. | -- Prepend remaining digits. | ||
ret = arg .. ret .. decimals | ret = arg .. ret .. decimals | ||
-- clear decimals | |||
decimals = '' | |||
return ret | return ret |
Revision as of 19:50, 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 decimals = ''
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.
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
-- 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
-- This could potentially break things if Lots is returned to another process.
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 = 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