Lua - leading zeros for integers

  • #1, z sebastianSaturday, 07. May 2016, 22:08 hodinky 9 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

    Kapitán

    2346 Posts


  • #2, z afrlmeSaturday, 07. May 2016, 22:42 hodinky 9 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.

    Imperátor

    7285 Posts

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

    Kapitán

    1582 Posts

  • #4, z sebastianSaturday, 07. May 2016, 23:07 hodinky 9 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?

    Kapitán

    2346 Posts

  • #5, z afrlmeSunday, 08. May 2016, 00:19 hodinky 9 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?

    Imperátor

    7285 Posts

  • #6, z SimonSSunday, 08. May 2016, 00:31 hodinky 9 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.

    Kapitán

    1582 Posts

  • #7, z sebastianSunday, 08. May 2016, 00:32 hodinky 9 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

    Kapitán

    2346 Posts

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

    Cheers Simon. wink

    Imperátor

    7285 Posts

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

    Kapitán

    2346 Posts