Check if input is a float

From Fire And Ice Grid
Jump to navigation Jump to search


This is a short LSL script snippet which will work in both Opensimulator and Second life. It takes user input and then verifies whether it can be correctly used as a float (decimal number).
If you prefer to download the file it is available along with others on my GitHub Page. This is a useful script to have in your LSL preprocessor folder.

Using The Script

The functionality for this script is contained in the method (sometimes known as user-defined function or UDF) at the top. Everything inside state_entry is there purely to demonstrate how the function might be used.

Script On Github

LSL And OSSL Script Library On GitHub

Licence

 1 BSD 3-Clause License
 2 Copyright (c) 2020, Sara Payne
 3 All rights reserved.
 4 Redistribution and use in source and binary forms, with or without
 5 modification, are permitted provided that the following conditions are met:
 6 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 7 2. Redistributions in binary form must reproduce the above copyright notice,
 8    this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 9 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
13 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
14 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
18 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Script

 1 integer CheckIsFloat (string inputString)
 2 {   //checks to see if the provided string can be type cast (converted) into a float
 3     list allowedChars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."]; //no other characters allowed in a decimal float other than "-" which is dealt with another way
 4     integer charOk = TRUE;
 5     if (llGetSubString (inputString, 0, 0) == "-") 
 6     {   //if this is a negative number thats ok, but we don't need it for the check, remove it. 
 7         inputString = llGetSubString(inputString, 1, -1); //removes the - from the start
 8     }//close if number is negative
 9     integer decimalIndex = llSubStringIndex(inputString, "."); //get the index of the decimal if one exists
10     if (decimalIndex != -1)
11     {   //only come here if the number has a decimal in it
12         string afterFirstDecimal = llGetSubString (inputString, decimalIndex+1, -1);
13         if (llSubStringIndex(afterFirstDecimal, ".") !=-1)  
14         {   //come here if there is more than one . in the string... aka not a valid decimal
15             charOk = FALSE; //set return value to false
16         }//close if there is more than one "." 
17     }//close if the string has a "."
18     integer charIndex = 0;
19     while (charOk && charIndex < llStringLength(inputString))
20     {   //loops through the characters in the string, checking each one is part of the allowed characters
21         //if any of them don't match charOK is set to false. 
22         string charToCheck = llGetSubString(inputString, charIndex, charIndex); //sets the current character to check
23         if(!(~llListFindList(allowedChars, (list)charToCheck))) //checks selected character against the allowed characters
24         {   //come here if the character is not in the allowed characters list. 
25             charOk = FALSE; //set result to FALSE
26         }//close if character is not in the allowed list
27         ++charIndex; //increase the character count   
28     }//close while loop
29     return charOk; // return TRUE/FALSE
30 }//close CheckIsFloat
31 
32 default
33 {
34     state_entry()
35     {
36         string userInputString = "123.567"; //sample input from a user
37         integer isFloat = CheckIsFloat(userInputString); //calling the checking method
38         float userInputFloat; // defines the new rotation for this example
39         if (isFloat) 
40         {   //if the method returned TRUE come here
41             userInputFloat = (float)userInputString; //typecast the string to a rotation
42         }//close if user input was a rotation
43         else 
44         {
45             //send error to user and get them to try again . 
46         }
47     }
48 }

Syntax Highlighting

GeShi syntax highlighting is enabled on this wiki. <be>

When using the tabs described in the link, change the language part to "lsl". Eg.
syntaxhighlight lang="lsl" line
inside angled brackets <>