Difference between revisions of "Category:LSL Integer"

From Second Life Wiki
Jump to navigation Jump to search
m (This second example is more how I test numbers. Doing character examination is silly.)
(More decimal string validations)
Line 36: Line 36:
}</lsl>
}</lsl>


Here's a simpler solution for strings containing positive or negative decimal integers (values from −2147483648 and 2147483647 written without + sign, leading zeros, or thousands separators ',' ).
Here's a simpler solution for strings containing positive or negative decimal integers (values from −2147483648 and 2147483647 written without + sign, leading zeros, or thousands separators ',' ). Omei Qunhua.


<lsl>
<lsl>
Line 42: Line 42:
         llOwnerSay("'" + data + "' contains a valid integer");
         llOwnerSay("'" + data + "' contains a valid integer");
</lsl>
</lsl>
The following examples will validate that a string contains only the characters 0 though 9. Omei Qunhua.
a) Example for a string of length 5
<lsl>
    StringOf5Dec(string test)
    {
        return ( (integer) ("1" + test) >= 100000);
    }
</lsl>
b) Example for a string of length 1 through 9
<lsl>
    VarStringIsDecimal(string test)
    {
        integer limit = (integer) llPow(10.0, llStringLength(test) );
        return ( (integer) ("1" + test) >= limit);
    }
</lsl>


==Further Reading==
==Further Reading==
For a more extensive coverage of integers, including the different ways they are used in LSL, see [[LSL_101/LSL_in_Focus:_Integers|LSL in Focus: Integers]].
For a more extensive coverage of integers, including the different ways they are used in LSL, see [[LSL_101/LSL_in_Focus:_Integers|LSL in Focus: Integers]].

Revision as of 07:32, 27 April 2014

Integers

The integer data type is a signed 32 bit value between −2,147,483,648 and +2,147,483,647 (that is 0x80000000 to 0x7FFFFFFF in hex). Integers are whole numbers. The fractional datatype is the float.

DEBUG_CHANNEL can be used as a constant for the maximum integer (for that is the value it is defined as).

Examples

All of the following are integers: <lsl>integer firstInt = 5512623; integer secondInt = ACTIVE; integer thirdInt = 0x61EC1A; integer fourthInt = -160693;</lsl>

The following are NOT integers, use float for them: <lsl> integer decimalValue = 125.2; // ERROR : Type mismatch -- Integer literals can't have a decimal. integer bigValue = 3147483647; // An undocumented way to say -1,147,483,649 // Integer literals can't be larger than 2,147,483,647. integer biggerValue = 10123456789; // An undocumented way to say -1 // Integer literals can't be larger than 2,147,483,647. </lsl>

The following function can be used to determine whether a string of characters consists only of integers. This can be important if you need to know that a user has entered a valid integer in chat or a textbox, for example. <lsl> integer IsInteger(string var) {

   integer i;
   for (i=0;i<llStringLength(var);++i)
   {
       if(!~llListFindList(["1","2","3","4","5","6","7","8","9","0"],[llGetSubString(var,i,i)]))
       {
           return FALSE;
       }
   }
   return TRUE;

}</lsl>

Here's a simpler solution for strings containing positive or negative decimal integers (values from −2147483648 and 2147483647 written without + sign, leading zeros, or thousands separators ',' ). Omei Qunhua.

<lsl>

   if ( (string) ( (integer) data) == data)
       llOwnerSay("'" + data + "' contains a valid integer");

</lsl>

The following examples will validate that a string contains only the characters 0 though 9. Omei Qunhua.

a) Example for a string of length 5 <lsl>

   StringOf5Dec(string test)
   {
       return ( (integer) ("1" + test) >= 100000);
   }

</lsl>

b) Example for a string of length 1 through 9 <lsl>

   VarStringIsDecimal(string test)
   {
       integer limit = (integer) llPow(10.0, llStringLength(test) );
       return ( (integer) ("1" + test) >= limit);
   }

</lsl>


Further Reading

For a more extensive coverage of integers, including the different ways they are used in LSL, see LSL in Focus: Integers.

Pages in category "LSL Integer"

The following 200 pages are in this category, out of 640 total.

(previous page) (next page)
(previous page) (next page)