How can i keep zero's in a string with numbers?
#1

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
Reply
#2

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;
}
Reply
#3

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.
Reply
#4

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)