Module:BigNumber

local p = {}

function round(what, precision) return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) end

function getLetter(exponent) letters = {'K','M','B','T','q','Q','s','S','O','N','d','U','D'}; if exponent == 0 then return ''; else auxExponent = exponent/3; auxExponent2 = math.floor((auxExponent-1)/13); auxExponent = (auxExponent-1)%13 + 1; letter = letters[auxExponent]; if auxExponent2 > 0 then return letter .. 'D' .. auxExponent2 else return letter; end end end

function p.parse(frame) return p._parse(frame.args[1]); end

function p._parse(number) num = tonumber(number); sign = ''; if num < 0 then num = -1*num; sign = '-'; digits = math.floor(math.log10(num)); elseif num > 0 then digits = math.floor(math.log10(num)); else digits = 0; end exponent = math.floor(digits/3)*3; units = num/math.pow(10,exponent); precision = 2 - (digits - exponent); units = round(units,precision); return sign .. units .. getLetter(exponent); end return p