Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

isNumeric() function

Bosozoku Kato
insurrectionist midget
Join date: 16 Jun 2003
Posts: 452
01-22-2006 17:28
Edit:
I trimmed the routine a little, now instead of doing string comparisons on every character I just typecast to an integer and only do string comparions if the value of the int is zero.
Edit2: Added check if string passed contains just a single minus sign "-", reordered nested if's more logically.
Edit3: oop I borked it, fixed now though :p ok ok I quit!

CODE
// isNumeric() checks a given string to see if it IS numeric
// It allows for negative values and floats.
// isNumeric() returns FALSE if the given string isn't numeric
// else it returns TRUE.
// Input (string) ending with a period is considered NON-numeric. (eg: "12.")
integer isNumeric(string sInput)
{
integer nLen = llStringLength(sInput) - 1; // len, zero based
string sChr;
integer nDecimalCount = 0;
integer nLoopCount = 0;
// initially check first char for "-" (negative, ignore (increment loopcounter)
if (llGetSubString(sInput, 0, 0) == "-")
{
nLoopCount = 1;
if (nLoopCount > nLen) return FALSE; // string passed is a single character "-"
}
while (nLoopCount <= nLen)
{
sChr = llGetSubString(sInput, nLoopCount, nLoopCount);
if ((integer)sChr == 0)
{
if (sChr == ".")
{
++nDecimalCount;
// if more than 1 ".", OR last character is a ".", false!
if (nDecimalCount > 1 || nLoopCount == nLen) return FALSE;
}
else if (sChr != "0")
{
return FALSE;
}
}
++nLoopCount;
}
return TRUE; // if we got this far, then it's numeric...
}
_____________________
float llGetAFreakingRealTimeStampSince00:00:00Jan11970();
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
01-22-2006 22:04
/54/0a/83971/1.html
_____________________
i've got nothing. ;)
Hiro Pendragon
bye bye f0rums!
Join date: 22 Jan 2004
Posts: 5,905
01-22-2006 22:27
Can we rename this, "isDecimal"?

FFBBAA is numeric, too. *grin*
_____________________
Hiro Pendragon
------------------
http://www.involve3d.com - Involve - Metaverse / Emerging Media Studio

Visit my SL blog: http://secondtense.blogspot.com