User:Toady Nakamura/Demo Typecast Random Float and Integer

From Second Life Wiki
Jump to: navigation, search

Demonstrates Typecasting... from float & integer to string

  • Floats are numbers with decimal places (3.1416 or 100.99)
  • Integers are round numbers (1, 3, 99, 2005)
  • Strings are anything enclosed in "quotes".

Here we take a number (randomly picked from zero to ten) and show you how to output it as both a float and integer.

That's called "type-casting"... changing one type of data to another.

Put this script in a prim and poke the prim to see float values and their corresponding integer values


default
{
    touch_start(integer detected_index)
    {
        float number = llFrand(10.0);  //pick a number between zero and the number in parenthesis
 
        string numString = (string)number;  // make a string to hold the answer
 
        llOwnerSay("The float value = " + numString ); // tell owner the value as float
 
 
 
        integer numInteger = (integer)number;  // convert the same number already picked
 
        numString = (string)numInteger; // to a string so it can be "said"
 
        llOwnerSay("The integer value = " + numString);  // tell owner the value as integer
    }
}


Visit my LSL wiki page for my library of simple scripts ! Toady Nakamura