SA-MP Forums Archive
How can i keep zero's in a string with numbers? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How can i keep zero's in a string with numbers? (/showthread.php?tid=523032)



How can i keep zero's in a string with numbers? - AIped - 30.06.2014

Well im making this new textdraw for money on screen.
I have this now;
pawn Код:
new mystring[10];
format(mystring,sizeof(mystring),"$%02d",pData[playerid][pCash]);
It shows the correct value but for example;
the string shows $100
but i actually want it to show $00000100 and so on.

I have read the wiki about it here https://sampwiki.blast.hk/wiki/Format
It doesnt explain the '02' part in %02d..maybe someone can explain what that does too ? as it might be related to my
question.

thanks in advance


Re: How can i keep zero's in a string with numbers? - Stinged - 30.06.2014

I was searching for your answer, as I really never tried making a money system with textdraw.
I found this, now of course, you have to re-write it in your code, as I just copy-pasted it.

pawn Код:
forward CheckMoney(playerid);
public CheckMoney(playerid)
{
    new string[128];
     
    if(GetPlayerMoney(playerid) < 10)
    {
        format(string, sizeof(string), "0000000%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(10 <= GetPlayerMoney(playerid) < 100)
    {
        format(string, sizeof(string), "000000%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(100 <= GetPlayerMoney(playerid) < 1000)
    {
        format(string, sizeof(string), "00000%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(1000 <= GetPlayerMoney(playerid) < 10000)
    {
        format(string, sizeof(string), "0000%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(10000 <= GetPlayerMoney(playerid) < 100000)
    {
        format(string, sizeof(string), "000%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(100000 <= GetPlayerMoney(playerid) < 1000000)
    {
        format(string, sizeof(string), "00%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(1000000 <= GetPlayerMoney(playerid) < 10000000)
    {
        format(string, sizeof(string), "0%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }

    if(10000000 <= GetPlayerMoney(playerid) < 999999999)
    {
        format(string, sizeof(string), "%iFt", GetPlayerMoney(playerid));
        TextDrawSetString(Ft[playerid], string);
        TextDrawShowForPlayer(playerid, Ft[playerid]);
        TextDrawShowForPlayer(playerid, Box[playerid]);
    }
    return 1;
}



Re: How can i keep zero's in a string with numbers? - Vince - 30.06.2014

Just replace the 2 in "%02d" with whatever length you want it to be. What this means is: make sure the output is always 2 characters long. If it isn't, pad it with zeros.


Re: How can i keep zero's in a string with numbers? - AIped - 30.06.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
Just replace the 2 in "%02d" with whatever length you want it to be. What this means is: make sure the output is always 2 characters long. If it isn't, pad it with zeros.
Yes i see that this way works most efficient thank you.