How can I check if a variable is a number? I'm using LINPUT to get a number, but if the input is a variable recieved is a string, the program gets an error. I was wondering if there is a was to verify if a string can be converted to a integer without error.
Choosing LINPUT because the ? from INPUT kinda sucks. And thank you.
And also, you can check if an string is actually a number with:
IF STR$(VAL(IN$))==IN$ THEN ' Is a number
MCGamer20000's code will check if certain strings are numbers. But, it will fail for strings with too many significant places such as "1.234567" (even integers "12345678901"), and strings in exponential format (e.g. VAL("1E5") is 100000). Also, hexadecimal values beginning with "&H" and binary values beginning with "&B".
You say VAL("1E5") is 100000, but it's just 1. Also, why would you want to allow the user to input hex or binary numbers like that...?
My code just needed to verify simple numbers like 150, and isn't 1e5 100000?
Just so everybody is clear, I believe 1E5 is shorthand for 1 x 10^5, as in scientific notation, not 1^5. Also, I typed in Direct mode, " PRINT VAL("1E5")" and SmileBasic calculates it as 100,000.
i use:
IF VAL(IN$)!=0 OR ASC(IN$)==48 THEN 'IS NUMBER
this works because VAL returns zero if the input isn't a number, and the number zero is ASCII character 48
Edit: I've obviously never tested that with a string which begins with a zero! Should test for string length: OR (ASC(IN$)==48 AND LEN(IN$)==1)
I see... It's not easy then. :(