SA-MP Forums Archive
How to separate numbers with a Comma - 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 to separate numbers with a Comma (/showthread.php?tid=449647)



How to separate numbers with a Comma - CrazyChoco - 09.07.2013

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");
                }



Re: How to separate numbers with a Comma - Vince - 09.07.2013

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;
}



Re: How to separate numbers with a Comma - CrazyChoco - 09.07.2013

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?


AW: How to separate numbers with a Comma - roym899 - 09.07.2013

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