function abb(n)
if n>=1*10^4 and n<1*10^6 then
n=(n/1*10^-3)
return string.sub(tostring(n),1,5).."K"
elseif n>=1*10^6 and n<1*10^7 then
n=(n/1*10^-6)
return string.sub(tostring(n),1,3).."M"
elseif n>=1*10^7 and n<1*10^8 then
n=(n/1*10^-7)
return string.sub(tostring(n),1,3).."B"
elseif n>=1*10^8 and n<1*10^9 then
n=(n/1*10^-8)
return string.sub(tostring(n),1,3).."T"
else
return n
end
end
print(abb(134444564))
This function take the parameter n and checks to see if it is between certain number ranges.
For example, the M statement is
if n>=1*10^4 and n<1*10^5 then
110^6 is scientific notation for 1000000
While 110^7 is 10000000
If n is between those two number I then take the number and turn it into a scientific number but without the exponent part at the end. EX:
local n = 1233344
n=(n/1*10^-6)
with n turned into this we can then go on to return the rounded number (not really round in this cause since im just taking the actual string characters) and then add the appropriate letter at the end
local n = 1.233344
return string.sub(tostring(n),1,3).."M"
string.sub is used to get the first 3 letters of the now string (tostring()). "1" "." "2" are the three "letters". This wont break if the number is like exactly 1 too.
For the thousands check I allowed there to be up to 5 letters since we could have like 444.4K as a number.
Please ask questions if you have any.
Edited to fix 1k to 999k range.
1 comment
Where Do I Put It?