AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Error with "pointing" a string number (https://forums.alliedmods.net/showthread.php?t=113805)

01101101 12-30-2009 10:12

Error with "pointing" a string number
 
I have a number, for example 1234. I want to show it like 1.234 (American people use 1,234)

I was trying with this
PHP Code:

    new str1[15]
    
num_to_str(numberstr114)
    new 
len strlen(str1)
    while(
len 3)
    {
        
len -= 3
        add
(str1len,".")
    } 

But that just produces random shit.

Owyn 12-30-2009 11:00

Re: Error with "pointing" a string number
 
http://www.amxmodx.org/funcwiki.php?go=func&id=48

01101101 12-30-2009 11:02

Re: Error with "pointing" a string number
 
Quote:

Originally Posted by Owyn (Post 1036524)

Whats wrong with it, I don't get it.

Mxnn 12-30-2009 13:43

Re: Error with "pointing" a string number
 
In the first line you have a "," plus.

01101101 12-30-2009 13:57

Re: Error with "pointing" a string number
 
Quote:

Originally Posted by Mxnn (Post 1036699)
In the first line you have a "," plus.

Nevermind, that was just a typo when I wrote the code here.

Mxnn 12-30-2009 18:36

Re: Error with "pointing" a string number
 
If you want to show the number you can do this:
PHP Code:

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

Test it. I don't remember if ^n is counted in strlen

EDIT: Is tested and it works for me.

joaquimandrade 12-30-2009 21:40

Re: Error with "pointing" a string number
 
Also tested and confirmed Mxnn. Anyway did my version of it (it should be a little faster):

PHP Code:

public convertNumber(numAsString[],numAsStringFormatted[])
{
    new 
len strlen(numAsString
    static 
startValue[] = {3,1,2}
    new 
start startValue[len 3]
    
    
copy(numAsStringFormatted,start,numAsString)
    
    if(
len 3)
    {
        new 
totalLen len + ((len-1) / 3)

        
add(numAsStringFormatted,totalLen,".")
        
        new 
i
        
for(i=start;i<len-3;i+=3)
        {
            
add(numAsStringFormatted,totalLen,numAsString[i],3)
            
add(numAsStringFormatted,totalLen,".")
        }
        
        
add(numAsStringFormatted,totalLen,numAsString[i],3)
    }



01101101 12-30-2009 23:27

Re: Error with "pointing" a string number
 
Yes, Mxnn version works fine. Haven't tried your version quim, dunno which will be faster in my case. I am "converting" 2 numbers which are 99% higher than 1000, and sometimes higher than 1000000, each time a players shoots and hits somebody. Considering that its a 32 slots zombie plague server in which people shoot like crazy, this, may affect (slightly) server perfonmance.

If I can, I'll profile both methods to see the difference.

Thanks both of you.


All times are GMT -4. The time now is 04:14.

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