SA-MP Forums Archive
[Include] [INC] FormatMoney() - 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: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] [INC] FormatMoney() (/showthread.php?tid=155801)



[INC] FormatMoney() - mick88 - 19.06.2010

This is a little include coded by me, you can paste it right into your gm. Hope you find it useful!

What this function does, is it formats money by inserting delimiter every 3 digits so it's easier to read and puts '$' before it. Default delimiter is comma. It also works with negative amounts and rounds float numbers! It returns a string.
It may be not the best solution to formatting money but from what I see it's the only one and it should suit most uses. It has no bugs, however maximum length is 15 characters + ending char ($-9,999,999,999 - minus 9 billion). You can increase this by changing define MAX_MONEY_STRING, but maximum of 3 delimiters will be inserted ($999,999,999,999).

Usage: FormatMoney(amount);

example:
Код:
printf("You received %s", FormatMoney(reward)); //default delimiter
printf("You paid %s", FormatMoney(price, " ")); //custom delimiter
Код:
stock FormatMoney(Float:amount, delimiter[2]=",")
{
	#define MAX_MONEY_STRING 16
	new txt[MAX_MONEY_STRING];
	format(txt, MAX_MONEY_STRING, "$%d", floatround(amount));
	new l = strlen(txt);
	if (amount < 0) // -
	{
	  if (l > 5) strins(txt, delimiter, l-3);
		if (l > 8) strins(txt, delimiter, l-6);
		if (l > 11) strins(txt, delimiter, l-9);
	}
	else
	{
		if (l > 4) strins(txt, delimiter, l-3);
		if (l > 7) strins(txt, delimiter, l-6);
		if (l > 10) strins(txt, delimiter, l-9);
	}
	return txt;
}
And bonus, this is what numbers formatted with this function look like:
Код:
     1 - $1
     10 - $10
    100 - $100
    1000 - $1,000
   10000 - $10,000
   100000 - $100,000
  1000000 - $1,000,000
  10000000 - $10,000,000
 100000000 - $100,000,000
 1000000000 - $1,000,000,000

Negative values:
     -1 - $-1
    -10 - $-10
    -100 - $-100
   -1000 - $-1,000
   -10000 - $-10,000
  -100000 - $-100,000
  -1000000 - $-1,000,000
 -10000000 - $-10,000,000
 -100000000 - $-100,000,000
-1000000000 - $-1,000,000,000



Re: [INC] FormatMoney() - SloProKiller - 19.06.2010

Awesome!!


Re: [INC] FormatMoney() - Calgon - 19.06.2010

It's only a function, I don't see why this was released as an include. You should release this in the "Useful Functions" thread, as this is simply just 1 function.


Re: [INC] FormatMoney() - mick88 - 19.06.2010

Quote:
Originally Posted by Calgon
It's only a function, I don't see why this was released as an include. You should release this in the "Useful Functions" thread, as this is simply just 1 function.
Right you are, sorry I didn't know about that thread, it should be sticked imho...


Re: [INC] FormatMoney() - Grim_ - 19.06.2010

Quote:
Originally Posted by mick88
Quote:
Originally Posted by Calgon
It's only a function, I don't see why this was released as an include. You should release this in the "Useful Functions" thread, as this is simply just 1 function.
Right you are, sorry I didn't know about that thread, it should be sticked imho...
It is.


Re: [INC] FormatMoney() - VinceQc - 19.06.2010

Good job.


Re: [INC] FormatMoney() - mick88 - 19.06.2010

Quote:
Originally Posted by Grim_
Quote:
Originally Posted by mick88
Quote:
Originally Posted by Calgon
It's only a function, I don't see why this was released as an include. You should release this in the "Useful Functions" thread, as this is simply just 1 function.
Right you are, sorry I didn't know about that thread, it should be sticked imho...
It is.
Oh, it's sticked in the Gamemode board. I wouldn't think of looking for it in there, in never look into Gamemodes...


Re: [INC] FormatMoney() - BlueRey - 20.06.2010


fix: http://bluerey.pastebin.com/e82A5hfU
pawn Код:
stock FormatMoney(Float:amount,delimiter[2]=",")
{
    #define MAX_MONEY_STRING 16
    new txt[MAX_MONEY_STRING];
    format(txt,MAX_MONEY_STRING,"%d",floatround(amount));
    new l = strlen(txt);
    if(amount < 0)
    {
         if(l >= 5) strins(txt,delimiter,l-3);
        if(l >= 8) strins(txt,delimiter,l-6);
        if(l >= 11) strins(txt,delimiter,l-9);
    }
    else
    {
        if(l >= 4) strins(txt,delimiter,l-3);
        if(l >= 7) strins(txt,delimiter,l-6);
        if(l >= 10) strins(txt,delimiter,l-9);
    }
    return txt;
}



Re: [INC] FormatMoney() - mick88 - 20.06.2010

Quote:
Originally Posted by BlueRey

fix: http://bluerey.pastebin.com/e82A5hfU
pawn Код:
stock FormatMoney(Float:amount,delimiter[2]=",")
{
    #define MAX_MONEY_STRING 16
    new txt[MAX_MONEY_STRING];
    format(txt,MAX_MONEY_STRING,"%d",floatround(amount));
    new l = strlen(txt);
    if(amount < 0)
    {
         if(l >= 5) strins(txt,delimiter,l-3);
        if(l >= 8) strins(txt,delimiter,l-6);
        if(l >= 11) strins(txt,delimiter,l-9);
    }
    else
    {
        if(l >= 4) strins(txt,delimiter,l-3);
        if(l >= 7) strins(txt,delimiter,l-6);
        if(l >= 10) strins(txt,delimiter,l-9);
    }
    return txt;
}
And what exactly did you "fix"? did you test your code? I tested mine, it works fine and I posted my results... .


Re: [INC] FormatMoney() - BlueRey - 22.06.2010

whatever you say...
I helped myself, i want to help others.
your code not working but my code working fine.


thanks anyway