# Any optimizations I could do for my number abreviator?

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
``````

The second elseif, specifically `elseif not string.find(tostring(number),"e+") then`, should be rewritten just as else. When a number is over 1,000 and is less than e+, it’ll do a second check to make sure it’s less than e+ which is unnecessary because you just checked.

You’re also creating a local variable called preAb and then just returning it andn ot using it for anything else other than that, you can cut out the middle man.

I see your points and I like them. I had the var preAb that meant pre abbreviation like the 52.42 of 52.42 K.

I wanted it to look cleaner.