Raised This Month: $ Target: $400
 0% 

large numbers, easy comma formula?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-02-2010 , 19:43   Re: large numbers, easy comma formula?
Reply With Quote #5

If your numbers will always be within the signed int32 range ( -2,147,483,648 to 2,147,483,647 ) you can use the below. If the numbers can be outside of this range then this will take a bit more work.

Edit: Discovered -2,147,483,648 is not possible since the positive variant of this is number is 2,147,483,648 which is larger than 2,147,483,647. abs() is used in this function which fails. I have edited the below accordingly.

Result
Code:
-2147483647 = -2,147,483,647

2147483647 = 2,147,483,647
Code from my post in the thread posted above:
PHP Code:
#include <amxmodx>

new iMin = -2147483647;
new 
iMax 2147483647;

public 
plugin_init() 
{
    new 
szNum15 ];
    
    
AddCommasiMin szNum charsmaxszNum ) );
    
server_print"%d = %s" iMin szNum );
    
    
AddCommasiMax szNum charsmaxszNum ) );
    
server_print"%d = %s" iMax szNum );
}

public 
AddCommasiNum szOutput[] , iLen )
{
    new 
szTmp15 ] , iOutputPos iNumPos iNumLen;
    
    if ( 
iNum )
    {
        
szOutputiOutputPos++ ] = '-';
        
iNum absiNum );
    }
    
    
iNumLen num_to_striNum szTmp charsmaxszTmp ) );

    if ( 
iNumLen <= )
    {
        
iOutputPos += copyszOutputiOutputPos ] , iLen szTmp );
    }
    else
    {
        while ( ( 
iNumPos iNumLen ) && ( iOutputPos iLen ) ) 
        {
            
szOutputiOutputPos++ ] = szTmpiNumPos++ ];
        
            if( ( 
iNumLen iNumPos ) && !( ( iNumLen iNumPos ) % ) ) 
                
szOutputiOutputPos++ ] = ',';
        }
        
        
szOutputiOutputPos ] = EOS;
    }
    
    return 
iOutputPos;

__________________

Last edited by Bugsy; 11-22-2015 at 07:05.
Bugsy is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:18.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode