Lua - leading zeros for integers

  • #1, by sebastianSaturday, 07. May 2016, 22:08 8 years ago
    Hey there again,

    I want to add a leading "0" before numbers which are smaller than 10 to deal with them in further functions.

    Aim is to let Integers look like that:
    01, 02, 03, 04, 05, 06, 07, 08, 09 , 10, 11, ...
    instead of
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...

    Is there an easy way to do it?

    kind regards
    Sebastian

    Thread Captain

    2346 Posts


  • #2, by afrlmeSaturday, 07. May 2016, 22:42 8 years ago
    No, not unless you make it a string. Numbers are numbers.

    You can read & manipulate both the integer & string values of a VS value using Lua script.

    Let's say you edit the integer value of a VS value & you want to display that value inside of a display text, object text or whatever, you could call an action after you change the value or even just an execute a script action part.
    If Values["test"].Int >= 0 and Values["test"].Int <= 9 then
     Values["test"].String = "0" .. Values["test"].Int
    else
     Values["test"].String = Values["test"].Int
    end
    


    I assume you already know how to include a value inside of a display text, no? Well for displaying the string value it's vs=value_name instead of v=value_name. See [url=http://wiki.visionaire-tracker.net/wiki/Displayed_Text]here[/here] for more details on display texts.

    Imperator

    7278 Posts

  • #3, by SimonSSaturday, 07. May 2016, 22:49 8 years ago
    Hi, faster way:
    Values["test"].String = string.format("%02d", Values["test"].Int)
    

    Thread Captain

    1580 Posts

  • #4, by sebastianSaturday, 07. May 2016, 23:07 8 years ago
    OH BOOIIIIII smile

    Yeah. String is allright as i need it anyway for strings (i want to go through all bookmarkxx.dat files for example.)

    So with the information i have now this should be possible:

    The int i is counting up in a "for loop"

    for i=1,99, 1 do
    ...
    filename = "bookmark".. string.format("%02d",i)..".dat";
    If file_exists(filename)  then... 
    


    should work, i guess?

    Thread Captain

    2346 Posts

  • #5, by afrlmeSunday, 08. May 2016, 00:19 8 years ago
    @ Simon: Ah nice... what's the format %02d do Simon? I completely forgot about formatting.

    @ Sebastian.204: Why are you checking if the bookmark (autosave/quicksave) files exist?

    Imperator

    7278 Posts

  • #6, by SimonSSunday, 08. May 2016, 00:31 8 years ago
    More intel on format here: http://www.cplusplus.com/reference/cstdio/printf/
    %d means output number (integer), %2d means reserve 2 letters for the number (inserts blanks) and %02d will use 0 instead of blanks.

    Thread Captain

    1580 Posts

  • #7, by sebastianSunday, 08. May 2016, 00:32 8 years ago
    I made a full working save manu with naming the entries a while ago and want to expand it from 8 to xx slots. Checking if the file exists is used to decide if i should load its corresponding name from an ini file which lists all entries. Because i dont want to hardcode every savestate checking (what i do right now) i thought it would be good to loop dat shit smile

    Thread Captain

    2346 Posts

  • #8, by afrlmeSunday, 08. May 2016, 00:42 8 years ago
    haha ok dokie mate. grin

    Cheers Simon. wink

    Imperator

    7278 Posts

  • #9, by sebastianSunday, 08. May 2016, 16:40 8 years ago
    when everything works fine, i'll try to show it here in the future smile

    Thread Captain

    2346 Posts