RosettaCodeData/Task/Determine-if-a-string-is-numeric/HicEst/determine-if-a-string-is-numeric.hicest
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

27 lines
1.5 KiB
Text

! = bin + 2*int + 4*flt + 8*oct +16*hex + 32*sci
isNumeric("1001") ! 27 = 1 1 0 1 1 0
isNumeric("123") ! 26 = 0 1 0 1 1 0
isNumeric("1E78") ! 48 = 0 0 0 0 1 1
isNumeric("-0.123") ! 4 = 0 0 1 0 0 1
isNumeric("-123.456e-78") ! 32 = 0 0 0 0 0 1
isNumeric(" 123") ! 0: leading blank
isNumeric("-123.456f-78") ! 0: illegal character f
FUNCTION isNumeric(string) ! true ( > 0 ), no leading/trailing blanks
CHARACTER string
b = INDEX(string, "[01]+", 128, Lbin) ! Lbin returns length found
i = INDEX(string, "-?\d+", 128, Lint) ! regular expression: 128
f = INDEX(string, "-?\d+\.\d*", 128, Lflt)
o = INDEX(string, "[0-7]+", 128, Loct)
h = INDEX(string, "[0-9A-F]+", 128, Lhex) ! case sensitive: 1+128
s = INDEX(string, "-?\d+\.*\d*E[+-]*\d*", 128, Lsci)
IF(anywhere) THEN ! 0 (false) by default
isNumeric = ( b > 0 ) + 2*( i > 0 ) + 4*( f > 0 ) + 8*( o > 0 ) + 16*( h > 0 ) + 32*( s > 0 )
ELSEIF(boolean) THEN ! 0 (false) by default
isNumeric = ( b + i + f + o + h + s ) > 0 ! this would return 0 or 1
ELSE
L = LEN(string)
isNumeric = (Lbin==L) + 2*(Lint==L) + 4*(Lflt==L) + 8*(Loct==L) + 16*(Lhex==L) + 32*(Lsci==L)
ENDIF
END