[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
#2

Awesome!!
Reply
#3

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.
Reply
#4

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...
Reply
#5

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.
Reply
#6

Good job.
Reply
#7

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...
Reply
#8


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;
}
Reply
#9

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... .
Reply
#10

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


thanks anyway
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)