SA-MP Forums Archive
Little question :) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Little question :) (/showthread.php?tid=576040)



Little question :) - bgedition - 31.05.2015

Hey all,
I have a question - How can I simplify this code? Is there any way?!

there is the code:
Код:
stock IntegerWithDelimiter(integer, delimiter[] = ",") { //32 bit digit, max - 2147483647 and on every 3 diggits adds delimiter
	new string[16];
	if(integer >= 0) {
		format(string, sizeof(string), "%d", integer);
		if(1000 <= integer < 10000) strins(string, delimiter, 2, sizeof(string));
		else if(10000 <= integer < 100000) strins(string, delimiter, 3, sizeof(string));
		else if(100000 <= integer < 1000000) strins(string, delimiter, 4, sizeof(string));
		else if(1000000 <= integer < 10000000) { strins(string, delimiter, 2, sizeof(string)); strins(string, delimiter, 6, sizeof(string)); }
		else if(10000000 <= integer < 100000000) { strins(string, delimiter, 3, sizeof(string)); strins(string, delimiter, 7, sizeof(string)); }
		else if(100000000 <= integer < 1000000000) { strins(string, delimiter, 4, sizeof(string)); strins(string, delimiter, 8, sizeof(string)); }
		else if(integer >= 1000000000) { strins(string, delimiter, 2, sizeof(string)); strins(string, delimiter, 6, sizeof(string)); strins(string, delimiter, 10, sizeof(string)); }
	} else {
		format(string, sizeof(string), "-%d", integer - (integer * 2));
		if(-1000 >= integer > -10000) strins(string, delimiter, 3, sizeof(string));
		else if(-10000 >= integer > -100000) strins(string, delimiter, 4, sizeof(string));
		else if(-100000 >= integer > -1000000) strins(string, delimiter, 5, sizeof(string));
		else if(-1000000 >= integer > -10000000) { strins(string, delimiter, 3, sizeof(string)); strins(string, delimiter, 7, sizeof(string)); }
		else if(-10000000 >= integer > -100000000) { strins(string, delimiter, 4, sizeof(string)); strins(string, delimiter, 8, sizeof(string)); }
		else if(-100000000 >= integer > -1000000000) { strins(string, delimiter, 5, sizeof(string)); strins(string, delimiter, 9, sizeof(string)); }
		else if(integer <= -1000000000) { strins(string, delimiter, 3, sizeof(string)); strins(string, delimiter, 7, sizeof(string)); strins(string, delimiter, 11, sizeof(string));}
	}
	return string;
}
please help...


Re: Little question :) - Konstantinos - 31.05.2015

PHP код:
IntegerWithDelimiter(integerdelimiter[] = ",")
{
    new
        
string[16],
        
count;
        
    
format(stringsizeof(string), "%d"integer);
    
    for (new 
strlen(string) - 10; --i)
    {
        if (++
count == 3)
        {
            
strins(stringdelimiterisizeof (string));
            
count 0;
        }
    }
    return 
string;




Re: Little question :) - Vince - 31.05.2015

One less variable:
PHP код:
formatnum(number, const separator[] = ",")
{
    
number clamp(number);
    new 
output[16];

    
valstr(outputnumber);

    for(new 
strlen(output) - 30-= 3)
    {
        
strins(outputseparatori);
    }
    return 
output;




Re: Little question :) - Konstantinos - 31.05.2015

I totally forgot about decreasing in the loop by 3 so no need to use the variable, thanks for reminding me.
Also just a note: valstr can crash the server with really high values so format is better for it.


Re: Little question :) - bgedition - 31.05.2015

Konstantinos your code works perfect but i wanna make something like this:
Код:
if(!strlen(delimiter) == 1) {
    strdel(delimiter, 0, strlen(delimiter));
    delimiter[0] = ",";
}
EDIT:
Vince's code isn't working with 32 bit digit

EDIT 2:
But it gives me errors and warnings


Re: Little question :) - bgedition - 01.06.2015

Hey all,

now the code is:
Код:
stock IntegerWithDelimiter(integer, delimiter[] = ",") { 
    new string[16], count; 
	format(string, sizeof(string), "%d", integer); 
	for(new i = strlen(string) - 1; i > 0; --i)  {
      Line 339: if(strlen(delimiter) != 1) strdel(delimiter, 0, strlen(delimiter)), strcat(delimiter, ",");
		if(++count == 3) {
			strins(string, delimiter, i, sizeof(string)); 
			count = 0; 
		} 
	} 
	return string; 
}
but I have warning N224 :
Код:
D:\Program Files\Rockstar Games\GTA San Andreas\server\pawno\include\bgedition.inc(339) : warning 224: indeterminate array size in "sizeof" expression (symbol "maxlength")
The line with the warning, the problem is in this -> strcat(delimiter, ",")


Re: Little question :) - Konstantinos - 01.06.2015

Both Vince's and my code work good. What are you trying to accomplish anyway?

EDIT: Okay, I see "-,100,000,000". Replace with:
pawn Код:
for (new i = strlen(string) - 1, j = ((integer < 0) ? 1 : 0); i > j; --i)
and it'll be fixed.

EDIT 2: Or even better as Vince tip:

pawn Код:
IntegerWithDelimiter(integer, delimiter[] = ",")
{
    new
        string[16];

    format(string, sizeof(string), "%d", integer);

    for (new i = strlen(string) - 3, j = ((integer < 0) ? 1 : 0); i > j; i -= 3)
    {
        strins(string, delimiter, i, sizeof (string));
    }
    return string;
}



AW: Little question :) - Kaliber - 01.06.2015

Just write it like this:

Код:
stock IntegerWithDelimiter(integer, delimiter[] = ",") 
{
    new str[24],i;
    valstr(str,integer),i=(integer > 0) ? strlen(str)-3 : strlen(str)-4;
    for(; i>0; i-=3) strins(str,delimiter,(integer > 0) ? i : i+1,24);
    return str;
}
Greekz


Re: Little question :) - bgedition - 01.06.2015

Kaliber thank you.
Konstantinos I want the max lenght of the delimiter to be 1 character. And if the lenght is more than 1 or is 0 to make the default character for delimiter. Because my server crashes if the delimiter's char is more than 1. And I know the crashing is from the small array size. I don't want to make it big array size. I think you can understand me.