SA-MP Forums Archive
Formatting numbers help! - 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: Formatting numbers help! (/showthread.php?tid=596496)



Formatting numbers help! - E7mad - 18.12.2015

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!


Re: Formatting numbers help! - Shoulen - 18.12.2015

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


Re: Formatting numbers help! - SickAttack - 18.12.2015

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



Re: Formatting numbers help! - Shoulen - 18.12.2015

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


Re: Formatting numbers help! - E7mad - 18.12.2015

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!


Re: Formatting numbers help! - Vince - 19.12.2015

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;