How to separate numbers with a Comma
#1

Hello,
Does anyone know how to make function, like if you reach above 3 digts it will automaticly seperate it with a comma.
Example: If i have 1500, instead of that it will be replaced with: 1,500.

If i wern't clear enough, i can explain it as best as i can.

My Code is:

pawn Код:
new Random = MRandom(100), str[60];
                if(Random > 25)
                {
                    new Random1 = (random(3500 - 1000) + 700);
                    GivePlayerMoney(playerid, Random1);
                    format(str, sizeof(str), "Money: %d", Random1);
                    SendClientMessage(playerid, -1, str);
                    SendClientMessage(playerid, -1, "Above 25");
                }

                if(Random <= 25)
                {
                    SendClientMessage(playerid, -1, "Less 25");
                }
Reply
#2

Had a need for it myself yesterday, so I wrote this:
pawn Код:
formatnum(number, const separator[] = ",")
{
    number = clamp(number);
    new output[16];
   
    valstr(output, number);

    for(new i = strlen(output) - 3; i > 0; i -= 3)
    {
        strins(output, separator, i);
    }
    return output;
}
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Had a need for it myself yesterday, so I wrote this:
pawn Код:
formatnum(number, const separator[] = ",")
{
    number = clamp(number);
    new output[16];
   
    valstr(output, number);

    for(new i = strlen(output) - 3; i > 0; i -= 3)
    {
        strins(output, separator, i);
    }
    return output;
}
I tried to see how it will work with the code you posted, but i got a problem. It doesn't go higher than 50 :/ even if it is setted to go up to 3500.

My code:

pawn Код:
new Random1 = (random(3500 - 1000) + 700);
GivePlayerMoney(playerid, Random1);
format(str, sizeof(str), "Money: %d", formatnum(Random1));
SendClientMessage(playerid, -1, str);
SendClientMessage(playerid, -1, "Above 25");
Any ideas why it won't?
Reply
#4

The function he posted returns a string, so you have to write "Money: %s" instead of "Money: %d".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)