Return to Index

# Function to test for a numeric input

February 18 2006 at 8:50 PM
Solitaire  (Login Solitaire1)
S

Number must be input as a string. Function tests the string and returns TRUE if the string can be converted into a real number, FALSE if the string contains any non-valid characters. It will work with either a positive or negative number, will accept a decimal point but reject more than one decimal point as invalid. At least one digit must be included.
--------------------------------------------------------------

DECLARE FUNCTION IsNumber% (snum AS STRING)
DIM snum AS STRING, dnum AS DOUBLE
CLS : INPUT "Please enter a number: ", snum\$
IF IsNumber%(snum\$) THEN
PRINT : PRINT snum\$; " is a real number."
dnum = VAL(snum\$)
PRINT : PRINT "The numeric value is:  "; dnum
ELSE
PRINT : PRINT snum\$; " is NOT a real number."
END IF
SYSTEM

FUNCTION IsNumber% (snum AS STRING)
CONST FALSE = 0, TRUE = NOT FALSE
DIM flag AS INTEGER, x AS INTEGER
DIM ch AS STRING, dot AS INTEGER
flag = FALSE: dot = 0
IF snum\$ = "" OR snum\$ = "." OR snum\$ = "-" OR snum\$ = "-." THEN
flag = TRUE
ELSE
FOR x = 1 TO LEN(snum\$)
ch\$ = MID\$(snum\$, x, 1)
IF ch\$ < "-" OR ch\$ > "9" OR ch\$ = "/" THEN flag = TRUE
IF x > 1 AND ch\$ = "-" THEN flag = TRUE
IF ch\$ = "." THEN dot = dot + 1
IF dot > 1 THEN flag = TRUE
NEXT x
END IF
IF flag = TRUE THEN
IsNumber% = FALSE
ELSE
IsNumber% = TRUE
END IF
END FUNCTION

 This message has been edited by Solitaire1 on Feb 22, 2006 7:43 PM

 Respond to this message
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement