19.06.2010, 18:31
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:
And bonus, this is what numbers formatted with this function look like:
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;
}
Код:
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


