## Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

# sgIsNumber -- Determine if a string contains a valid float.

Bob Bauhaus
Fictional being
Join date: 22 Sep 2004
Posts: 24
02-13-2005 22:16
The following I whipped up to make sure that a string really contains a numeric value, so that (float) fooString gives an accurate value. No, it doesn't do any overflow checking, but it can handle integers (1234 and -567, decimals (.12 and -5.01), and exponential notation (-54.78E+12) while catching something like "3.6duct tape56", which would falsely return just 3.6 with the typecasting. Of course, if you don't want exponents, or decimals, or even negatives, it should be trivially easy to disable the appropriate code.

CODE
`list sgNumbers = ["0","1","2","3","4","5","6","7","8","9"];integer sgIsNumber(string val){//TODO: Determine if the value is a number?list result;integer eSpot;string numberString;	eSpot = llSubStringIndex(llToUpper(val),"E");//Determine if exponent is valid.	if (eSpot > 0) {		if (-1 < llSubStringIndex("+-",llGetSubString(val,eSpot + 1,eSpot + 1))) {			numberString = llGetSubString(val,eSpot + 2, -1);		} else {			numberString = llGetSubString(val,eSpot + 1, -1);		}		if(numberString == "") {return 0;}		if([] != llParseString2List(numberString,sgNumbers,[])) {return 0;}		val = llGetSubString(val,0,eSpot - 1);	}	if ([] == llParseString2List(val,[".","-"],[])) {return 0;} //Make sure we have SOME numbers.	return (-1 < llListFindList(["-","."],llParseString2List(val,sgNumbers,[])));}`

In case you're wondering, sg is a prefix that I'm using with a project of mine, like how ll is the LSL prefix.
Psyke Phaeton
Psyke's Defense Systems
Join date: 12 Oct 2003
Posts: 197
03-02-2005 09:42
What about converting the string to an integer/float and back to a string and comparing?:

CODE
`if (str_val == (string)((integer)str_val)) { llSay(0,"Is a Integer"); }`

or
CODE
`if (str_val == (string)((float)str_val)) { llSay(0,"Is a Float"); }`

UPDATE: The float version will only work if the correct number of decimal places is used. Maybe extra 0's and a decimal point could be added to the string in various combinations and then tested.
Psyke Phaeton
Psyke's Defense Systems
Join date: 12 Oct 2003
Posts: 197
03-02-2005 09:44
test
Cenji Neutra
www.apez.biz
Join date: 30 Oct 2004
Posts: 36
03-05-2005 12:44
llParseString2List(string,seperator list,spacer list) only allows the separators list to contain a maximum of 8 strings, but sgNumbers contains 10. Is that a problem?
Deus
Join date: 9 Jan 2004
Posts: 1,483
03-06-2005 13:07
From: Psyke Phaeton
What about converting the string to an integer/float and back to a string and comparing?:

CODE
`if (str_val == (string)((integer)str_val)) { llSay(0,"Is a Integer"); }`

or
CODE
`if (str_val == (string)((float)str_val)) { llSay(0,"Is a Float"); }`

UPDATE: The float version will only work if the correct number of decimal places is used. Maybe extra 0's and a decimal point could be added to the string in various combinations and then tested.

For floats;

if(str == llGetSubString((string)((float)str),0,llStringLength(str))) { llSay(0,"Is float."; }

_____________________
GigasSecondServer
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
03-07-2005 06:59
For floats;

if(str == llGetSubString((string)((float)str), 0, llStringLength(str))) { llSay(0,"Is float."; }

The trouble with this is that E can move the decimal place.

a related library function i've writen is FloatToSci. It's not all that fast but it will create a float in string form in scientific notation that is exactly the same.

If you read the bottom of that thread there are some instructions for relaxing the output standard it uses. This allows for the code to go faster (if no user ever has to read the floats it produces it's a good idea)
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey