Author Topic: Does a Is Number function exist in QB64?  (Read 9779 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Does a Is Number function exist in QB64?
« on: December 21, 2018, 10:28:42 am »
So I needed something like this (sloppy I know but works for now, I'll rerturn to it later to clean up, unless one already exists)
Code: QB64: [Select]
  1. FUNCTION IS_NUMBER (NUM AS STRING) ' returns 1 if string is an integer, 0 if not
  2.     DIM TEMPNUMBER AS INTEGER
  3.     DIM HAYSTACK AS STRING
  4.     DIM I, j AS INTEGER
  5.  
  6.     NUM = UCASE$(NUM)
  7.     '// simplified number check
  8.     '// Useful for getting a user input where one answer needs to be determined if
  9.     '// if it a string answer or a numeric input
  10.     HAYSTACK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-,?/|<>][{}:;'" + CHR$(32) + CHR$(34)
  11.     '// If the NUM string has any of the above characters it is not a numb
  12.     FOR I = 1 TO LEN(NUM)
  13.         FOR j = 1 TO LEN(HAYSTACK)
  14.             IF INSTR(MID$(HAYSTACK, j, I), MID$(NUM, I, 1)) > 0 THEN
  15.                 IS_NUMBER = 0
  16.                 EXIT FUNCTION
  17.             END IF
  18.         NEXT
  19.     NEXT
  20.  
  21.     IS_NUMBER = 1
  22.  

Just checks a string input to see if it is a number or not, 1 = yes 0 = no simple

Does QB64 have something like this?
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Does a Is Number function exist in QB64?
« Reply #1 on: December 21, 2018, 10:38:43 am »
Not natively.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #2 on: December 21, 2018, 10:46:43 am »
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))
« Last Edit: December 21, 2018, 11:12:17 am by Cobalt »
Granted after becoming radioactive I only have a half-life!

Marked as best answer by xra7en on December 21, 2018, 09:45:40 am

FellippeHeitor

  • Guest
Re: Does a Is Number function exist in QB64?
« Reply #3 on: December 21, 2018, 10:49:27 am »
You could probably check if LTRIM$(RTRIM$(STR$(VAL(num$)))) = LTRIM$(RTRIM$(num$)).

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #4 on: December 21, 2018, 11:41:46 am »
You could probably check if LTRIM$(RTRIM$(STR$(VAL(num$)))) = LTRIM$(RTRIM$(num$)).

OO that is nice...
I whipped this up at 3am after my cats woke me up LOL

for some reason , many years ago, i thought there was a one liner like that.. let me try that one. makes more sense.
I love writing text games, and sometimes you have to make an option fo 1 - 100 or R-eturn, Q-uit or a array of other string answers mixed with numeric within the same input. PHP has one like this


EDIT:

KISS IS GOOD! thanks.

« Last Edit: December 21, 2018, 11:46:09 am by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #5 on: December 21, 2018, 11:43:20 am »
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))

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
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Does a Is Number function exist in QB64?
« Reply #6 on: December 21, 2018, 11:51:08 am »
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?
« Last Edit: December 21, 2018, 01:33:01 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #7 on: December 21, 2018, 12:44:23 pm »
Two comments:

1. in OP code, you comment the function to check for integer that is something like:
Code: QB64: [Select]
  1. if number = int(number) then isInteger = true

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.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #8 on: December 21, 2018, 01:28:30 pm »
Two comments:

1. in OP code, you comment the function to check for integer that is something like:
Code: QB64: [Select]
  1. if number = int(number) then isInteger = true

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.

I don't see the
Code: QB64: [Select]
  1. if number = int(number) then isInteger = true
However VERY VERY good point - Unless I am writing some "clicker"/"idle" game I do not work with numbers quite that high, however, to cover all bases, that is something to check.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Does a Is Number function exist in QB64?
« Reply #9 on: December 21, 2018, 01:33:33 pm »
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?

My point exactly.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #10 on: December 21, 2018, 03:12:46 pm »
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?

Yes, and simpler :-)
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: Does a Is Number function exist in QB64?
« Reply #11 on: December 21, 2018, 03:14:00 pm »
Friendly reminder that _TRIM$ hasn't made it into the stable build and is available only if you're beta testing the dev build.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #12 on: December 21, 2018, 06:12:19 pm »
Friendly reminder that _TRIM$ hasn't made it into the stable build and is available only if you're beta testing the dev build.

thanks for the update...
no rush here! QB64 is way flexible enough to adjust for it :-)

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #13 on: December 22, 2018, 12:00:43 am »

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

that is the thing about games as opposed to applications, you have to deal with a lot more checking.
Granted after becoming radioactive I only have a half-life!

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: Does a Is Number function exist in QB64?
« Reply #14 on: December 29, 2018, 10:57:06 am »
One thing that I think has been overlooked here is to test for negative numbers, in other words, look for a minus sign, as well as a decimal point.

I've used this code for the last 30 years or so -- it's a bit clumsy, but is an easy bit of fire-and-forget code that gets copied into just about every program that I write.  (Which isn't many, these days.)

In other words, you want to test for ASCII 45 and ASCII 46, but want to eliminate ASCII 47 ( the "/" character).

The jump to "notanumber" in the code below basically links to a bit of code that first erases the invalid text, and then beeps.

Code: QB64: [Select]
  1. FOR i = 1 TO LEN(text$)
  2. IF ASC(MID$(text$, i, 1)) < 45 OR ASC(MID$(text$, i, 1)) >= 58 GOTO notanumber
  3. ' It's not a number, minus sign, / or . character, so goto notanumber
  4. IF ASC(MID$(text$, i, 1)) = 47 GOTO notanumber
  5. REM: it's a / character, so goto notanumber
  6.  
Malcolm