[Include] [INC] FormatMoney()
#1

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
Reply


Messages In This Thread
[INC] FormatMoney() - by mick88 - 19.06.2010, 18:31
Re: [INC] FormatMoney() - by SloProKiller - 19.06.2010, 18:36
Re: [INC] FormatMoney() - by Calgon - 19.06.2010, 18:38
Re: [INC] FormatMoney() - by mick88 - 19.06.2010, 20:28
Re: [INC] FormatMoney() - by Grim_ - 19.06.2010, 21:45
Re: [INC] FormatMoney() - by VinceQc - 19.06.2010, 22:19
Re: [INC] FormatMoney() - by mick88 - 19.06.2010, 22:43
Re: [INC] FormatMoney() - by BlueRey - 20.06.2010, 01:24
Re: [INC] FormatMoney() - by mick88 - 20.06.2010, 15:37
Re: [INC] FormatMoney() - by BlueRey - 22.06.2010, 05:27

Forum Jump:


Users browsing this thread: 3 Guest(s)