Power as int, not as text
Posted: Wed Jul 05, 2017 10:28 am
Hi!
We have now
power ="60kW"
Is it possible to make same power as
power = 60000?
We have now
power ="60kW"
Is it possible to make same power as
power = 60000?
Code: Select all
power = string.format("%dW", <Formula>)
Code: Select all
power = value.."W"
Code: Select all
power = tostring(value).."W"
Code: Select all
local factor = 0.01
local EquipmentTypes =
{
["night-vision-equipment"] = true,
["energy-shield-equipment"] = true,
["battery-equipment"] = true,
["solar-panel-equipment"] = true,
["generator-equipment"] = true,
["active-defense-equipment"] = true,
["movement-bonus-equipment"] = true,
["roboport-equipment"] = true,
["belt-immunity-equipment"] = true,
}
local function rebalance(path, parameter, oldValue)
if type(oldValue) == "string" then
local num, str, exp, energy = string.match(oldValue, "([%-+]?[0-9]*%.?[0-9]+)(([kKMGTPH]?)([WJ]))")
--log(tostring(parameter)..": "..tostring(oldValue).." split: "..tostring(num)..", "..tostring(str)..", "..tostring(exp)..", "..tostring(energy))
if num and str and energy then
local correctedNum = tonumber(num) * factor
path[parameter] = correctedNum..str
end
end
end
local function parse_table(t)
for k, v in pairs(t) do
if type(v) == "table" then
parse_table(t[k])
else
rebalance(t, k, v)
end
end
return ct
end
for equipment, flag in pairs(EquipmentTypes) do
for name, item in pairs(data.raw[equipment]) do
--log("found "..tostring(name))
local path = data.raw[equipment][name]
parse_table(path)
end
end
Code: Select all
function text_to_int (text)
if not text then return end
local tabl = {["kW"] = 1000, ["MW"] = 1000000, ["GW"] = 1000000000}
for i, v in pairs (tabl) do
if string.find(text, i) then
local a = string.gsub(text, i, "") -- example: "kW" is replaced by ""
local num = tonumber(a) * v
return num
end
end
return false
end
Code: Select all
function multiplicate_string (str, factor)
local value = tonumber(string.match(str, "%d+")) * factor
local unit = string.match(str, "%a+")
local new_str = value..unit -- for example 600 .. "kW" --> "600kW"
return new_str
end