Neverworld Grid

You are not logged in. Would you like to login or register?



6/25/2019 6:29 pm  #1


Check if input is a FLOAT - ideal to include in your preprocessor

Method to check if a given string can be typecast to a float. Use independently or add the method to your LSL preprocessor folder and add an include statement to your script. 

#include "chkisfloat.lsl"
integer isFloat = ChkIsFloat (string inputString)

integer ChkIsFloat (string inputString)
{   //takes the input string and makes sure it only contains numbers and at most 1 decimal place
    list allowedChars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
    integer charOk = TRUE;
    if (llGetSubString (inputString, 0, 0) == "-") 
    {   // remove any minus sign at the start if present
        inputString = llGetSubString(inputString, 1, -1);
    }//close remove minus sign
    integer decimalIndex = llSubStringIndex(inputString, ".");
    if (decimalIndex != -1)
    {   // come here if we have a decimal in the string
        string afterFirstDecimal = llGetSubString (inputString, decimalIndex+1, -1);
        if (llSubStringIndex(afterFirstDecimal, ".") !=-1)  
        {   //come here if a second decimal is found in the string
            charOk = FALSE;
        }//close if a 2nd decimal found
    }//close if we have a decimal
    integer i = 0;
    while (charOk && i < llStringLength(inputString))
    {   //loops through the whole string making sure each char is valid, stops if invalid char found 
        string charToCheck = llGetSubString(inputString, i, i);
        if(!(~llListFindList(allowedChars, (list)charToCheck)))
        {   //come here if character not found in list
            charOk = FALSE;
        }
        ++i;   
    }//close while
    return charOk; 
}//close chk is a float 

Last edited by sara (6/26/2019 3:46 am)

 

Board footera

 

Powered by Boardhost. Create a Free Forum