Formatting numbers help!
#1

Hello everyone.
I want a function "stock" to format cash or any number.
Example, If money = 1565105, after using "AFM", It will be 1,565,105
I want the script of "format", here is an example:
Код:
money = 1766918
format(string, sizeof(string), "You have paid $%s, good for you! :D", AFM(money);
SendClientMessage(playerid, COLOR_WHITE, string);
The message will be like:
"You have paid $1,766,918, good for you! "

Thanks for reading,
I help you help me!
Reply
#2

Use https://sampwiki.blast.hk/wiki/Strlen to get the length of the string and then determine where the commas need to go

Use https://sampwiki.blast.hk/wiki/Strins to insert the "," in to the string.

That's how I would do it.

Create a function that returns the new string
Reply
#3

https://sampwiki.blast.hk/wiki/AddCommas
https://sampforum.blast.hk/showthread.php?tid=184328

pawn Код:
stock ConvertToAmount(value)
{
    new string[13], count = -1;
    valstr(string, value);

    for(new i = strlen(string); i > 0; i --)
    {
        count ++;

        if(count == 3)
        {
            strins(string, ",", i);
           
            count = 0;
        }
    }
    return string;
}
Reply
#4

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Thank you
+rep
Reply
#5

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/AddCommas
https://sampforum.blast.hk/showthread.php?tid=184328

pawn Код:
stock ConvertToAmount(value)
{
    new string[13], count = -1;
    valstr(string, value);

    for(new i = strlen(string); i > 0; i --)
    {
        count ++;

        if(count == 3)
        {
            strins(string, ",", i);
           
            count = 0;
        }
    }
    return string;
}
Thank you very much, useful!
Thank you Shoulen too!
Reply
#6

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Rather crappy function, though. Can be written much shorter. I don't know about others, but I think the most logical approach is to start from the back. Therefore:
PHP код:
AddThousandsSeparators(number, const separator[] = ",")
{
    new 
output[16];
    
format(outputsizeof(output), "%d"number);
    
    for(new 
pos strlen(output) - 3pos 0pos -= 3)
        
strins(outputseparatorpos)
        
    return 
output;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)