Difference between revisions of "LSL 101/Integers"

From Second Life Wiki
Jump to navigation Jump to search
Line 2: Line 2:
{{NavNextPrev|prev=Strings and Simple Output|next=The touch_start Event}}
{{NavNextPrev|prev=Strings and Simple Output|next=The touch_start Event}}


Integers are whole numbers from −2,147,483,648 and +2,147,483,647.  Integers can be represented literally in a scripts or placed within a [https://wiki.secondlife.com/wiki/LSL_101/Variables variable] at runtime. To be able to see these numbers however, you'll need to [https://wiki.secondlife.com/wiki/Typecast typcast] then into a string.  
Integers are another data type found in LSL along with Strings. LSL can handle whole numbers from −2,147,483,648 to +2,147,483,647.  Integers can either be represented literally in a script or placed within a [https://wiki.secondlife.com/wiki/LSL_101/Variables variable] at runtime. Integers can use the following mathematial operations.
 
+ add
- subtract
* multiply
/ divide
% modulus
 
 
To be able to print these numbers however, you'll need to [https://wiki.secondlife.com/wiki/Typecast typcast] then into a string.  




Line 30: Line 39:
</lsl>
</lsl>


Here 5 is represented as a string so no typecasting is needed. However in this form it's not really the integer 5 (which can be added and subtracted). Here's an example where math is performed before printing.
In the example above, 5 is represented as a string so no typecasting is needed. However in this form it's not really the integer 5. If it's not an integer, you won't be able to perform math on the number. Here's an example where math is performed before printing.


<lsl>
<lsl>
Line 38: Line 47:
     {
     {
         llSay(0, (string)(2+3) );
         llSay(0, (string)(2+3) );
    }
 
}
</lsl>
With some [https://wiki.secondlife.com/wiki/LSL_101/String_Concatenation string concatenation] you can combine two strings (including typecast numbers) to help label the output.
<lsl>
default
{
    state_entry()
    {
        llSay(0, "two plus three equals " + (string)(2 + 3) );
        llSay(0, "100 minus 1 equals " + (string)(100 - 1) );
        llSay(0, "5 times 5 equals " + (string)(5 * 5) );
        llSay(0, "six divides in twelve " + (string)(12 / 6) + " times." );
        llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
     }
     }
    
    
}
}
</lsl>
</lsl>

Revision as of 05:58, 29 September 2009

← Strings and Simple Output ↑̲  LSL 101  ̲↑ The touch_start Event →

Integers are another data type found in LSL along with Strings. LSL can handle whole numbers from −2,147,483,648 to +2,147,483,647. Integers can either be represented literally in a script or placed within a variable at runtime. Integers can use the following mathematial operations.

+ add - subtract

  • multiply

/ divide % modulus


To be able to print these numbers however, you'll need to typcast then into a string.


<lsl> default {

   state_entry()
   {
       llSay(0, (string)5 );
   }
  

} </lsl>

Why do we preface the number 5 with (string)? llSay is only expecting a string and will not be able to print a number without you typcasting it first. We can do the following.


<lsl> default {

   state_entry()
   {
       llSay(0, "5" );
   }
  

} </lsl>

In the example above, 5 is represented as a string so no typecasting is needed. However in this form it's not really the integer 5. If it's not an integer, you won't be able to perform math on the number. Here's an example where math is performed before printing.

<lsl> default {

   state_entry()
   {
       llSay(0, (string)(2+3) );
   }
  

} </lsl>

With some string concatenation you can combine two strings (including typecast numbers) to help label the output.

<lsl> default {

   state_entry()
   {
       llSay(0, "two plus three equals " + (string)(2 + 3) );
       llSay(0, "100 minus 1 equals " + (string)(100 - 1) );
       llSay(0, "5 times 5 equals " + (string)(5 * 5) );
       llSay(0, "six divides in twelve " + (string)(12 / 6) + " times." );
       llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
   }
  

} </lsl>