Function to return clean 'currency' value. -
I'm trying to create a function that expects an integer and returns a string (?) that has formatted the number so it reads good (I will only use this function for SendClientMessage's).
Re: Function to return clean 'currency' value. -
Код:
FormatNumber(number)
{
new Str[15];
format(Str, 15, "%d", number);
if(strlen(Str) < sizeof(Str))
{
if ( number >= 1000 && number < 10000 )
strins( Str, ",", 1, sizeof( Str ) );
else if ( number >= 10000 && number < 100000 )
strins( Str, ",", 2, sizeof( Str ) );
else if ( number >= 100000 && number < 1000000 )
strins( Str, ",", 3, sizeof( Str ) );
else if ( number >= 1000000 && number < 10000000 )
strins( Str, ",", 1, sizeof( Str ) ),strins( Str, ",", 5, sizeof( Str ) );
else if ( number >= 10000000 && number < 100000000 )
strins( Str, ",", 2, sizeof( Str ) ),strins( Str, ",", 6, sizeof( Str ) );
else if ( number >= 100000000 && number < 1000000000 )
strins( Str, ",", 3, sizeof( Str ) ),strins( Str, ",", 7, sizeof( Str ) );
else if ( number >= 1000000000 && number < 10000000000 )
strins( Str, ",", 1, sizeof( Str ) ),
strins( Str, ",", 5, sizeof( Str ) ),
strins( Str, ",", 9, sizeof( Str ) );
else format( Str, 10, "%d", number );
}
else format( Str, 15, "error" );
return Str;
}
Re: Function to return clean 'currency' value. -
PHP код:
FormatNumber(number, prefix[] = "$")
{
static value[32], length;
format(value, sizeof(value), "%d", (number < 0) ? (-number) : (number));
if((length = strlen(value)) > 3)
{
for(new i = length, l = 0; --i >= 0; l ++)
{
if ((l > 0) && (l % 3 == 0)) strins(value, ",", i + 1);
}
}
if(prefix[0] != 0) strins(value, prefix, 0);
if(number < 0) strins(value, "-", 0);
return value;
}
I'm using this one, found it onto the forum a while ago, though I don't know who made it, or which topic it came from.