Little question :)
#1

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...
Reply
#2

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;

Reply
#3

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;

Reply
#4

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.
Reply
#5

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
Reply
#6

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, ",")
Reply
#7

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;
}
Reply
#8

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
Reply
#9

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


Forum Jump:


Users browsing this thread: 1 Guest(s)