Code: Select all
local energy_chars =
{
k = 10^3,
K = 10^3,
M = 10^6,
G = 10^9,
T = 10^12,
P = 10^15,
E = 10^18,
Z = 10^21,
Y = 10^24
}
function util.parse_energy(energy)
local ending = energy:sub(energy:len())
if not (ending == "J" or ending == "W") then
error(ending.. " is not a valid unit of energy")
end
local multiplier = (ending == "W" and 1 / 60) or 1
local magnitude = energy:sub(energy:len() - 1, energy:len() - 1)
if tonumber(magnitude) then
return tonumber(energy:sub(1, energy:len()-1)) * multiplier
end
multiplier = multiplier * (energy_chars[magnitude] or error(magnitude.. " is not valid magnitude"))
return tonumber(energy:sub(1, energy:len()-2)) * multiplier
end
Code: Select all
function util.format_number(amount, append_suffix)
local suffix = ""
if append_suffix then
local suffix_list =
{
["T"] = 1000000000000,
["B"] = 1000000000,
["M"] = 1000000,
["k"] = 1000
}
for letter, limit in pairs (suffix_list) do
if math.abs(amount) >= limit then
amount = math.floor(amount/(limit/10))/10
suffix = letter
break
end
end
end
local formatted, k = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted..suffix
end
It's really not that hard to write a function that converts a fuel value in long format to the short form again, so I've done that. But is it really necessary that everybody reinvents the wheel if there already is a common library that all mods can use? I'm well aware that the game takes care of the formatting eventually, so no matter what format we use when setting fuel_value, the players will get to see it properly formatted. However, it would be a really useful addition for modders!
Readability is very important, especially during debugging. If I change a setting, I want to check that it has been set correctly, and it really makes a difference whether or not I can see at a glance that a setting has an unreasonable value. Counting zeroes unnecessarily is boring, and prone to mistakes. Therefore, I'd like to see such a function officially implemented. Would you do that, please?