Oposite of util.parse_energy(energy)

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
lovely_santa
Filter Inserter
Filter Inserter
Posts: 502
Joined: Sat Feb 18, 2017 9:41 pm
Contact:

Oposite of util.parse_energy(energy)

Post by lovely_santa »

Hi,

util.parse_energy(energy) will revert a string to a value. While creating some localised_description with additional values, I came across the need of converting an int value to it's string value (localised version then). Bilka said, when finished I should ask to add this to the core game lualib.

The function:

Code: Select all

  local prefix = {
    {"si-prefix-symbol-kilo"},
    {"si-prefix-symbol-mega"},
    {"si-prefix-symbol-giga"},
    {"si-prefix-symbol-tera"},
    {"si-prefix-symbol-peta"},
    {"si-prefix-symbol-exa"},
    {"si-prefix-symbol-zetta"},
    {"si-prefix-symbol-yotta"}
  }
  
function util.createPrefixedValue(value, localisedUnitSymbol, roundPresicion)
  -- roundPresicion defaults to 0 (aka integer value)

  local prefixIndex = 0
  while (value > 1000 and prefixIndex < #prefix) do
    value = value / 1000
    prefixIndex = prefixIndex + 1
  end
  prefix = prefix[prefixIndex]

  if roundPresicion then -- make sure it is an uint value
    roundPresicion = math.floor(roundPresicion + 0.5)
    roundPresicion = roundPresicion > 0 and roundPresicion or nil
  end

  return {"",
    string.format(roundPresicion and "%."..roundPresicion.."f " or "%i ", value),
    prefix or localisedUnitSymbol, prefix and localisedUnitSymbol or nil
  }
end
  • It will parse the value and add the correct prefix symbol (for example 1001 converts to 1k, while 500 will not have a prefix)
  • The passed localisedUnitSymbol will add the unit to the value, can be hardcoded as well, could also be empty ("" or nil).
  • Code probably will need to be converted to use _ instead of capitalized characters
Examples:

Code: Select all

util.createPrefixedValue(123456789, {"si-unit-symbol-watt"})
    => ingame shows as "123 MW"
    
util.createPrefixedValue(123456789, "W", 3)
    => ingame shows as "123.456 MW"
    
util.createPrefixedValue(2/3, {"si-unit-symbol-watt"}, 3)
    => ingame shows as "0.667 W"
Kind regards
lovely_santa
Last edited by lovely_santa on Sat Jul 27, 2019 5:47 pm, edited 1 time in total.
You can find all my mods on the mod portal. Also helping on Arch666Angel's mods.
Image

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Oposite of util.parse_energy(energy)

Post by Boodals »

The code can be optimized a bunch. There's no reason to create the lookup table every time you call the function, and the while loop can be flattened using logarithms I believe.

User avatar
lovely_santa
Filter Inserter
Filter Inserter
Posts: 502
Joined: Sat Feb 18, 2017 9:41 pm
Contact:

Re: Oposite of util.parse_energy(energy)

Post by lovely_santa »

The lookup table can indeed be put outside the actual function; which I updated the initial code post.
And the while loop could be changed to if elseif ... else end case, but that's what i've put in discord first, and was requested to change...
You can find all my mods on the mod portal. Also helping on Arch666Angel's mods.
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Oposite of util.parse_energy(energy)

Post by eradicator »

I don't know what you talked about on discord so i don't know the precise requirements, but i've used something like this so far. It'll treat any garbage input as "0" to avoid errors, not sure if raising errors is preferred here.
Edit1: Also not sure why you need the ability to use a custom override for the prefix, but i've put it in there anyway. Ah, *cough* i see. Lemme fix that...
Edit2: Fixed a few bugs along the way. Should now work for negative numbers too.

Code: Select all

local misc = {} --rename to util or whatever
local prefixes = {
  [3 ] = "kilo" , [6 ] = "mega" , [9 ] = "giga" ,
  [12] = "tera" , [15] = "peta" , [18] = "exa"  ,
  [21] = "zetta", [24] = "yotta",  
  }
function misc .power_string_from_number(number,unit,precision)
  precision = math.max(0,math.floor(tonumber(precision) or 0))
  number    = tonumber(number) or 0
  local l = (number == 0) and 1 or math.floor(math.log(math.abs(number),10))
  local n = math.min(24,l - (l % 3))
  return {
    "",
    string.format("%."..precision.."f",number/10^n),
    prefixes[n] and {"si-prefix-symbol-"..prefixes[n]} or unit,
    prefixes[n] and unit or nil
  }
end
  
Examples:

Code: Select all

local function test(x) print(x[2],x[3][1],(x[4] or {})[1]) end --[[out-of-game testing]]
test(misc .power_string_from_number(1234.567890,{"si-unit-symbol-watt"}))
test(misc .power_string_from_number(12345670,{"si-unit-symbol-watt"},2))
test(misc .power_string_from_number(1234567890,{"si-unit-symbol-watt"},2))
test(misc .power_string_from_number(12345600000000000000000000000007890,{"si-unit-symbol-watt"},2))
test(misc .power_string_from_number(-12.6667,{"si-unit-symbol-watt"},2))
test(misc .power_string_from_number("a",{"si-unit-symbol-watt"},-2))
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding interface requests”