I'm playing a mud which uses inhumanely huge numbers. In an effort to curtail the onslaught of numbers on my screen, I wrote a function that converts values like 8,000,000,000 into "8 billion".
I'm using a division method, and it works well when paired with string.format to narrow the result down to only one decimal place, like 8.3 billion. My problem is that string.format also likes to make a whole number a decimal, so 100 billion becomes 100.0 billion.
I tried fixing it with string.gsub(string, "%.0", ""), but it didn't seem to do anything to the results -- I'm still getting 100.0 million. Anyone know of a good solution? I really don't want to do math.floor or rounding it down or anything like that, since if there is a useful decimal like 8.6, I want to keep it.
Function follows:
I'm using a division method, and it works well when paired with string.format to narrow the result down to only one decimal place, like 8.3 billion. My problem is that string.format also likes to make a whole number a decimal, so 100 billion becomes 100.0 billion.
I tried fixing it with string.gsub(string, "%.0", ""), but it didn't seem to do anything to the results -- I'm still getting 100.0 million. Anyone know of a good solution? I really don't want to do math.floor or rounding it down or anything like that, since if there is a useful decimal like 8.6, I want to keep it.
Function follows:
function truncate (s_trunc)
s_trunc = string.gsub(s_trunc, ",", "")
n_trunc = tonumber(s_trunc)
if (string.len(s_trunc) >= 1) and (string.len(s_trunc) <= 3) then
return s_trunc
elseif (string.len(s_trunc) >= 4) and (string.len(s_trunc) <= 6) then
n_divide = 1000
n_class = "thousand"
elseif (string.len(s_trunc) >= 7) and (string.len(s_trunc) <= 9) then
n_divide = 1000000
n_class = "million"
elseif (string.len(s_trunc) >= 10) and (string.len(s_trunc) <= 12) then
n_divide = 1000000000
n_class = "billion"
elseif (string.len(s_trunc) >= 13) and (string.len(s_trunc) <= 15) then
n_divide = 1000000000000
n_class = "trillion"
elseif (string.len(s_trunc) >= 16) and (string.len(s_trunc) <= 18) then
n_divide = 1000000000000000
n_class = "quadrillion"
elseif (string.len(s_trunc) >= 19) and (string.len(s_trunc) <= 21) then
n_divide = 1000000000000000000
n_class = "quintillion"
elseif (string.len(s_trunc) >= 22) and (string.len(s_trunc) <= 24) then
n_divide = 1000000000000000000000
n_class = "sextillion"
elseif (string.len(s_trunc) >= 25) and (string.len(s_trunc) <= 27) then
n_divide = 1000000000000000000000000
n_class = "septillion"
elseif (string.len(s_trunc) >= 28) and (string.len(s_trunc) <= 30) then
n_divide = 1000000000000000000000000000
n_class = "octillion"
else
return "error"
end
return string.gsub(string.format("%.1f", n_trunc/n_divide), "%.0", "").." "..n_class
end