Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Forcing a integer to be 3 digits?

Keno Pontoppidan
Registered User
Join date: 20 Oct 2005
Posts: 75
11-28-2006 10:13
Say I did something in a script and it gave out a integer 26, how would I force that to become
026 or if I got 3 force it to be 003.
Dimentox Travanti
DCS Coder
Join date: 10 Sep 2006
Posts: 228
11-28-2006 10:16
Use strings? and typecast for conversions?
_____________________
LSL Scripting Database - http://lsl.dimentox.com
Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
11-28-2006 10:19
Yeah, simplest way is probably just to test if it's a number less than 10, if it is then cast to a string and add two zeroes. If it isn't, test to see if it's less than 100 and cast to string and add a zero if it is.
_____________________
Computer (Mac Pro):
2 x Quad Core 3.2ghz Xeon
10gb DDR2 800mhz FB-DIMMS
4 x 750gb, 32mb cache hard-drives (RAID-0/striped)
NVidia GeForce 8800GT (512mb)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-28-2006 10:41
You could also look in the wiki at llCeil, llFloor, and llRound
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-28-2006 11:34
Uhhh.. folks.. he's asking about zero-padding, not rounding or type conversion. :P

Try this:

CODE

paddedstring = llGetSubString("00"+(string)numberdata,-3,-1);


Note that such won't work for negative numbers; you will have to do something different if you want zero-padded negative integer fields.
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
11-28-2006 16:26
You can't have a zero-padded integer, so you have to typecast to a string and fake it. Here's the method I used:

CODE

integer number = 3;
string pad_num = (string)number;
if (number < 10) pad_num = "00" + pad_num;
else if (number < 100) pad_num = "0" + pad_num;
llOwnerSay("Zero-padded number is: " + pad_num);

or a version to handle negatives:
CODE

integer number = -3;
string pad_num = (string)llAbs(number);
if (llAbs(number) < 10) pad_num = "00" + pad_num;
else if (llAbs(number) < 100) pad_num = "0" + pad_num;
if (number < 0) pad_num = "-" + pad_num;
llOwnerSay("Zero-padded number is: " + pad_num);
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-28-2006 17:12
From: DoteDote Edison
You can't have a zero-padded integer, so you have to typecast to a string and fake it. Here's the method I used:


I never said you could. :)

That was the whole basis of the answer, too. :)
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
11-28-2006 17:39
From: Talarus Luan
Note that such won't work for negative numbers; you will have to do something different if you want zero-padded negative integer fields.

Yup, just a small change though
CODE

string pad( integer Value ) {

if( Value < 0 ) { return "-" + pad( -Value ); }
else { return llGetSubString("00"+(string)numberdata,-3,-1); }
}
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-28-2006 17:55
Well, if the field *has* to be 3 characters, it would be slightly more complicated than that, though.

I was going to wait and see if he needed to deal with negative numbers first before I suggested a way to deal with them. ;)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
11-28-2006 18:03
ah it still needs to be tweaked a little more also. I am sure he gets the idea by now but 26 was in the original question also. So would need to add a if(Value > 9 && Value < 100) and the other stuff that goes with it. Sorry, time for Eureka on SciFI!!!!!
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
11-28-2006 18:24
From: Talarus Luan
Well, if the field *has* to be 3 characters, it would be slightly more complicated than that, though.

Ah yes, slightly ^^
CODE

string pad( integer Value ) {

if( Value < 0 ) { return "-" + llGetSubString(pad( -Value ), -2, -1); }
else { return llGetSubString("00"+(string)numberdata,-3,-1); }
}

edit: it'd obviously get all broken with numbers like -100 or less, but that can't be really helped with this sort of limitations
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-28-2006 18:36
Or this (look ma! no recursion!): ;)

CODE

string pad(integer Value) {
if (Value < 0)
return llGetSubString("-0"+(string)(-Value),-3,-1);
else
return llGetSubString("00"+(string)Value,-3,-1);
}
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
11-29-2006 01:58
or a more general purpose solution

CODE

// EDIT : The following two lines where misse din original post
string zeros = "000000000";
string stars = "**********";
// End of EDIT

string ZeroPaddedString(integer value, integer width)
{
integer iVal = llAbs(value);
string strvalue = (string)iVal;
if(llStringLength(strvalue) > width)
{
strvalue = llGetSubString(stars, - width, -1 );
}
else
{
if(value < 0)
{
strvalue = "-" + llGetSubString(zeros + strvalue, - (width - 1), -1 );

}

else
{
strvalue = llGetSubString(zeros + strvalue, - width, -1 );
}

}
return strvalue;
}


Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-29-2006 04:20
From: Talarus Luan
Or this (look ma! no recursion!): ;)

CODE

string pad(integer Value) {
if (Value < 0)
return llGetSubString("-0"+(string)(-Value),-3,-1);
else
return llGetSubString("00"+(string)Value,-3,-1);
}

That isn't going to work.

Try:
CODE

string pad(integer Value) {
if (Value < 0)
return "-" + llGetSubString("0"+(string)(-Value),-2,-1);
else
return llGetSubString("00"+(string)Value,-3,-1);
}


Newgate has the right idea; but it looses the sign when truncating an integer.

A rewritten version (that i think will work).
CODE

string ZeroPaddedString(integer value, integer width)
{
string strvalue = (string)value;
string zeros;
integer len = ~-llStringLength(strvalue);
while((len = -~ len) < width)
zeros += "0";
return llGetSubString(llInsertString(strvalue, len = (value < 0), zeros), len - width, ~len);
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
11-29-2006 05:17
From: Strife Onizuka


Newgate has the right idea; but it looses the sign when truncating an integer.

A rewritten version (that i think will work).
CODE

string ZeroPaddedString(integer value, integer width)
{
string strvalue = (string)value;
string zeros;
integer len = ~-llStringLength(strvalue);
while((len = -~ len) < width)
zeros += "0";
return llGetSubString(llInsertString(strvalue, len = (value < 0), zeros), len - width, ~len);
}



Strife,
Sorry for being dense here but where would I be loosing the sign apart from in the llAbs call?

Also not sure if its a typo/paste error but while((len = -~ len) < width) ????
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
11-29-2006 05:58
From: Newgate Ludd
Strife,
Sorry for being dense here but where would I be loosing the sign apart from in the llAbs call?

Also not sure if its a typo/paste error but while((len = -~ len) < width) ????


CODE

if(llStringLength(strvalue) > width)
{
strvalue = llGetSubString(stars, - width, -1 );
}


1) where is stars defined?
2) assuming you meant strvalue, strvalue lacks a sign at that point; so the sign is dropped.

Yeah len = -~ len is a bit evil i came up with; it has the same effect as doing ++len. But it turns out to be faster. I'm a glutton for punishment when it comes to writing scripts where there are marginal gains in optimization. It also uses fewer bytes of memory (while no raindrop is responsible for the flood, if you reduce the amount of water just a bit, it may not overflow the dam).

~- is minus 1
-~ is plus 1

say you have
c = a - b - 1;
you can reduce that to
c = a + ~b;
this works because...
c = a - (b + 1);
c = a - (-~b);
c = a + ~b;
in this instance, the savings in speed and memory is quite noticeable (6 bytes of memory ^_^ or a 24% reduction in overhead for the line)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
11-29-2006 06:19
From: Strife Onizuka
CODE

if(llStringLength(strvalue) > width)
{
strvalue = llGetSubString(stars, - width, -1 );
}


1) where is stars defined?
2) assuming you meant strvalue, strvalue lacks a sign at that point; so the sign is dropped.


Ahhh, screw up on my part, stars, and zeros should both have been defined at the top of the code, my cut n paste seems to have missed them.

CODE
string zeros = "000000000";
string stars = "**********";


From: Strife Onizuka
Yeah len = -~ len is a bit evil i came up with; it has the same effect as doing ++len. But it turns out to be faster. I'm a glutton for punishment when it comes to writing scripts where there are marginal gains in optimization. It also uses fewer bytes of memory (while no raindrop is responsible for the flood, if you reduce the amount of water just a bit, it may not overflow the dam).

~- is minus 1
-~ is plus 1

say you have
c = a - b - 1;
you can reduce that to
c = a + ~b;
this works because...
c = a - (b + 1);
c = a - (-~b);
c = a + ~b;
in this instance, the savings in speed and memory is quite noticeable (6 bytes of memory ^_^ or a 24% reduction in overhead for the line)


CLONK penny drops. complement operator? didnt realise LSL supported it.

I agree with you whole heartedly about optimisation, although i do tend to be (overly) verbose when posting here purely for readability.
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-29-2006 20:44
From: Strife Onizuka
That isn't going to work.

Try:
CODE

string pad(integer Value) {
if (Value < 0)
return "-" + llGetSubString("0"+(string)(-Value),-2,-1);
else
return llGetSubString("00"+(string)Value,-3,-1);
}


Yeah, I made a small mistake there, but it was written kinda as a lark, since it wasn't part of the problem specification to begin with.

I've got field pad functions (with space, zero, or anychar) which handle any-sized fields without a problem. If it was needed, I could've posted one of those instead. ;)

From: someone
Newgate has the right idea; but it looses the sign when truncating an integer.

A rewritten version (that i think will work).
CODE

string ZeroPaddedString(integer value, integer width)
{
string strvalue = (string)value;
string zeros;
integer len = ~-llStringLength(strvalue);
while((len = -~ len) < width)
zeros += "0";
return llGetSubString(llInsertString(strvalue, len = (value < 0), zeros), len - width, ~len);
}


try this instead:

CODE

string ZeroPad = "000000000";

string LeftPadInteger(integer Value, integer Width) {
if (Value < 0)
return "-" + llGetSubString(ZeroPad+(string)(-Value),1-Width,-1);
else
return llGetSubString(ZeroPad+(string)Value,-Width,-1);
}


The only thing that might be necessary to add is value-checking Width.