You could probably check if LTRIM$(RTRIM$(STR$(VAL(num$)))) = LTRIM$(RTRIM$(num$)).
so you want to check if the string is only a number or if it contains a number?
'Checking that text is all numbers
Function IS_NUMBER(TXT$)
for I%= 1 to len(TXT$)
test$=mid$(TXT$, I%,1)
if asc(test$)>=48 and asc(test$)<=57 then Good%%=-1 else Good%%=0
if NOT Good%% then I%=LEN(TXT$)+1
next I%
IS_NUMBER=Good%%
end function
'Checking if it contains Numbers
Function HAS_NUMBER(TXT$)
for I%= 1 to len(TXT$)
test$=mid$(TXT$, I%,1)
if asc(test$)>=48 and asc(test$)<=57 then Good%%=-1 else Good%%=0
if Good%% then I%=LEN(TXT$)+1
next I%
HAS_NUMBER=Good%%
end function
its pretty simple, but yeah as Fellippe says there is no built in function for that.
now if you wanted to allow for decimal points '.' you would simply add that check to the IF THEN. (ASC(46))
Two comments:
1. in OP code, you comment the function to check for integer that is something like:Code: QB64: [Select]
2. For most generic IsNumber function watch out for letters used for exponents in float types, E or D (I don't usually deal with numbers that big or small). Might get around this with Fellippe and Steve's examples but check.
FUNCTION IsNum (text$)
a$ = _TRIM$(text$)
b$ = _TRIM$(STR$(VAL(text$)))
IF a$ = b$ THEN IsNum = 1
END FUNCTION
Will the above work for your needs?
FUNCTION IsNum (text$)
a$ = _TRIM$(text$)
b$ = _TRIM$(STR$(VAL(text$)))
IF a$ = b$ THEN IsNum = 1
END FUNCTION
Will the above work for your needs?
Friendly reminder that _TRIM$ hasn't made it into the stable build and is available only if you're beta testing the dev build.
very nice.. let me tear this apart and check it out as well. the decimal is what I added in mine. While most users are normal, my HAYSTACK does not check for ALL googy stuff players might try to input (games are more prone to people enter strings to find an exploit that regular aps) LOL
here is what I did with your code... what do you think?Code: QB64: [Select]
IS_NUMBER = 1 ' It's not a number, minus sign, / or . character, so goto notanumber ' it's a / character, so goto notanumber NEXT i
here is what I did with your code... what do you think?
Out of interest, I'd be interested in seeing a piece of code that used this function. As I've just typed elsewhere, I almost never use functions, and would appreciate seeing an example using a bit of code I know and understand.
(If you're wondering why, it's chiefly that early BASICs made functions difficult (to me, anyway), and by the time QB45 came along, I'd got out of the habit. Plus, the QB45 IDE doesn't exactly make the experience all that user-friendly, either.)
Malcolm
For example, is 1E15 a valid number?