There's also the problem (Edit: as mentioned by Kakjens) with small remainders giving a result of x.00 Mil. so it could be extended too:
Lua Code:
--function for beautiful numbers
local floor = math.floor
local strlen, strsub = string.len, string.sub
local function ReadableNumber(num)
local ret
local placeValue = ("%%.%df"):format(2) --probably can be improved into "%.0f"
local prefix = ""
if not num then
return "0 "
elseif num >= 1000000000000 then
prefix = " Tril" -- trillion
if mod(num, 1000000000000) < 10000000000 then
ret = floor(num / 1000000000000)
else
ret = placeValue:format(num / 1000000000000)
end
elseif num >= 1000000000 then
prefix = " Bil" -- billion
if mod(num, 1000000000) < 10000000 then
ret = floor(num / 1000000000)
else
ret = placeValue:format(num / 1000000000)
end
elseif num >= 1000000 then
prefix = " Mil" -- million
if mod(num, 1000000) < 10000 then
ret = floor(num / 1000000)
else
ret = placeValue:format(num / 1000000)
end
elseif num >= 1000 then
prefix = "k" -- thousand
ret = placeValue:format(num / 1000)
else
ret = num
end
local len = strlen(ret)
if len > 3 then
if strsub(ret, len-2, len-2) == "." and strsub(ret, len) == "0" then
ret = strsub(ret, 1, len-1)
end
end
return ret..prefix
end