AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Add some points on a Integer (https://forums.alliedmods.net/showthread.php?t=121092)

meTaLiCroSS 03-11-2010 13:30

Add some points on a Integer
 
How i can do something like this on a integer:

165 -> 165 (rofl)
3450 -> 3.450
172834 -> 172.834
1938294029 -> 1.938.294.029

(being a string, obviusly)

Thanks in advance. (:

worldspawn 03-11-2010 13:32

Re: Add some points on a Integer
 
sorry for offtop but what for?

Emp` 03-11-2010 13:49

Re: Add some points on a Integer
 
Code:

InsertCharacter( szInput[], iLoc, iChar, szOutput[], iLen )
{
    copy(szOutput, iLen, szInput);
    if( iLoc < iLen )
    {
        szOutput[iLoc] = iChar;
        copy(szOutput[iLoc+1], iLen-(iLoc+1), szInput[iLoc]);
    }
}

Untested. Usage:
Code:

InsertCharacter( "12345", 2, '.', blah, charsmax(blah));
// blah == "12.345"


meTaLiCroSS 03-11-2010 13:58

Re: Add some points on a Integer
 
Quote:

Originally Posted by worldspawn (Post 1114586)
sorry for offtop but what for?

Why i must tell you? o.O

Quote:

Originally Posted by Emp` (Post 1114606)
Code:

InsertCharacter( szInput[], iLoc, iChar, szOutput[], iLen )
{
    copy(szOutput, iLen, szInput);
    if( iLoc < iLen )
    {
        szOutput[iLoc] = iChar;
        copy(szOutput[iLoc+1], iLen-(iLoc+1), szInput[iLoc]);
    }
}

Untested. Usage:
Code:

InsertCharacter( "12345", 2, '.', blah, charsmax(blah));
// blah == "12.345"


What is the usage of the second parameter?

Seta00 03-11-2010 13:59

Re: Add some points on a Integer
 
At which character index you want to insert the third parameter.
E.g.: 2 means insert third parameter after the second char of the string (index 2)

meTaLiCroSS 03-11-2010 14:34

Re: Add some points on a Integer
 
It works, but i don't need that...

I need if can be done automatically, something like this:

PHP Code:

InsertPointChars(13274buffercharsmax(buffer)) // buffer = "13.274"
InsertPointChars(200000buffercharsmax(buffer)) // buffer = "200.000"
InsertPointChars(58963214buffercharsmax(buffer)) // buffer = "58.963.214" 

On every 3 last numbers...

Emp` 03-11-2010 15:22

Re: Add some points on a Integer
 
Code:

InsertPointChars( num, szOutput[], iLen )
{
    num_to_str( num, szOutput, iLen );
    for( new len = strlen(szOutput)-3; len > 0; len-=3 )
    {
        copy(szOutput[len+1], iLen-(len+1), szOutput[len]);
        szOutput[len] = '.';
    }
}

Untested.

Exolent[jNr] 03-11-2010 18:28

Re: Add some points on a Integer
 
Oh I see what you want.
You don't use periods for this, you use commas.

Code:
AddCommas(const input[], output[], const len) {     new stop = contain(input, ".");         if(stop == -1)     {         stop = strlen(input);     }         new o, i;     while(i < stop && o < len)     {         output[o++] = input[i++];                 if(o < len && i < stop && ((stop - i) % 3) == 0)         {             output[o++] = ',';         }     }         if(o < len)     {         o += copy(output[o], len - o, input[stop]);     }         return o; }

Works for both integers and floats.

Code:
new iNum = 123456; new szNum[ 12 ]; num_to_str( iNum, szNum, charsmax( szNum ) ); new szNumComma[ 15 ]; AddCommas( szNum, szNumComma, charsmax( szNumComma ) ); client_print( 0, print_chat, "%i is %s", iNum, szNumComma );

Code:
new Float:fNum = 123.456; new szNum[ 16 ]; float_to_str( fNum, szNum, charsmax( szNum ) ); new szNumComma[ 19 ]; AddCommas( szNum, szNumComma, charsmax( szNumComma ) ); client_print( 0, print_chat, "%f is %s", fNum, szNumComma );

meTaLiCroSS 03-11-2010 20:09

Re: Add some points on a Integer
 
Oh damn, you rocks Exolent :crab:

I'll test later, because i am not on my pc :gyar:

Thanks Emp` too, maybe i'll use your stock for other things (:

Mxnn 03-11-2010 20:50

Re: Add some points on a Integer
 
If you want a shorter stuff.
PHP Code:

    //Number must be the number that you want to pointed (integrer)
    
new str[15], strpointed[15], len
    num_to_str
(numberstr14)
    
len=strlen(str)
    new 
c
    
    
for (new i=0;i<len;i++) {
        if (
i!=&& ( (len-i)%3==0) ) {
            
add(strpointed14"."1)
            
c++
            
add(strpointed[i+c], 1str[i], 1)
        }
        else
            
add(strpointed[i+c], 1str[i], 1)
    }
//strpointed is the number with '.' 



All times are GMT -4. The time now is 19:49.

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