LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Checking variable type

Root / Programming Questions / [.]

joelableCreated:
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.

How can I check if a variable is a number?
http://smilebasicsource.com/page?pid=51
I'm using LINPUT to get a number, but if the input is a variable recieved is a string, the program gets an error.
There is no 'if'. LINPUT only uses string variables. INPUT can be used with numeric variables. Why are you choosing LINPUT over INPUT?
I was wondering if there is a was to verify if a string can be converted to a integer without error.
To convert a string to a numerical value, use VAL, e.g. VAL("5.77") gives the numerical value 5.77. It does not evaluate expressions: VAL("3-2") gives 3, and if the string does not begin with a number e.g. VAL("Four score and 20"), the result is 0.

Choosing LINPUT because the ? from INPUT kinda sucks. And thank you.

Choosing LINPUT because the ? from INPUT kinda sucks. And thank you.
You can remove the ? from INPUT; just use a , to separate the leading string from the inputs instead of a ;. Example: INPUT "",VAR% drops a blank prompt in front of you.

I see now. Thanks.

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?

You say VAL("1E5") is 100000, but it's just 1.
You say that as if it were true.
Also, why would you want to allow the user to input hex or binary numbers like that...?
I don't know, really. But someone might, and I don't want them to suffer confusion and frustration due to my lack of imagination. I'm trying to be considerate.
My code just needed to verify simple numbers like 150
It will work for all integers, represented in decimal, with 15 or fewer digits.

It will work for all integers, represented in decimal, with 15 or fewer digits.
Okay, that's basically all I needed.

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)

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 don't think there's a need for that extra check - after all, you could have "00" (which is 0), and you'd probably never get "0text" or anything like that. Question: what does VAL("12 dogs") evaluate to? 0 or 12?

Question: what does VAL("12 dogs") evaluate to? 0 or 12?
0 (in PetitComputer, though, 12)

I see... It's not easy then. :(

I see... It's not easy then. :(
It's even less easy than that. VAL("12 dogs") evaluates to zero. VAL("12 giraffes") evaluates to twelve. I have just put in a bug report on Miiverse.