Function to return clean 'currency' value.
#1

Hello there,

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).

What will be a good way to do this?

PHP код:
FormatNumber(number)
{
    
// Ex. convert '10000' to '$10,000'
    
return converted;

Thanks in regards
Reply
#2

Код:
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;
}
use %s
Reply
#3

PHP код:
FormatNumber(numberprefix[] = "$")
{
    static 
value[32], length;

    
format(valuesizeof(value), "%d", (number 0) ? (-number) : (number));

    if((
length strlen(value)) > 3)
    {
        for(new 
length0; -->= 0++)
        {
            if ((
0) && (== 0)) strins(value","1);
        }
    }
    if(
prefix[0] != 0strins(valueprefix0);
    if(
number 0strins(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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)