Ive made my own number abreviator(1234 to 1.234 K). It can convert numbers like 10342344 to 10 M and can convert scientific notation into a abbreviated number aswell. It works really well but I wonder if I could add any optimizations?
SCRIPT:
function abreviate(number)
local abreviations = {
[3] = "K",
[6] = "M",
[9] = "B",
[12] = "T",
[15] = "Qa",
[18] = "Qi",
[21] = "Sx",
[24] = "Se",
[27] = "Oc",
[30] = "Nn"
}
if number < 1000 then
return number
elseif string.find(tostring(number),"e+") then
local split = string.split(tostring(number),"e+")
if split[1] and split[2] then
local power = tonumber(split[2])
if power then
local abreviation = power - (power % 3)
local truepre = tonumber(split[1])
if truepre then
local preAb = tostring(truepre * (10^(power-(abreviation)))) .. " "
return preAb .. abreviations[abreviation]
end
else
return number
end
end
elseif not string.find(tostring(number),"e+") then
local num = tostring(math.floor(number))
local length = #num
local trueLength = #num % 3
if trueLength == 0 then
trueLength = 3
end
local abreviation = abreviations[length-trueLength]
if abreviation then
local startNum = string.sub(num, 1, trueLength) .. "."
local decimals = string.sub(num, trueLength+1,trueLength+2) .. " "
if startNum and decimals then
return startNum .. decimals .. abreviation
else
return number
end
else
return number
end
end
end