[futurebasic] Re: [FB] FN IsNumeric

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Robert Purves <robert.purves@...>
Date: Mon, 29 Oct 2001 09:12:06 +1300
>Jay, unable to resist the challenge, "purtifies"-- and possibly
>"fastifies"-- RP code:
>
>>RP said:
>>  >Your function does not understand signs or exponential notation, nor does
>>  >it acknowledge that leading/trailing spaces are "harmless extras".
>>  >Something more elaborate is needed, given the wide range of numeric forms
>>  >that can be understood by FB^3's val function. Try this:
>>  >
>>Well, not that much more elaborate. Here's my entry. I don't know how it
>>compares for speed, but it uses only register vars, and if nothing else
>>it's purtier! :-)
>>
>>0"0
>>=J= a  y
>>           "
>>PS. This still doesn't allow hex, octal, or binary values that FB
>>understands, but I've reached my limit for tonight


>I ask you Robert Purves: Should this function handle these other
>bases to be truly "numeric"?


Jay's IsNumeric gets my vote. The Profiler shows my version to be a little
faster but not by enough to compensate for its ugliness. (FWIW, my
IsNumeric began life in ZBasic and migrated through FB and FBII to FB^3. In
its older forms it used a LONG FN GetNextChar, which shortened the code
greatly, with no major speed hit.)

The main use of functions like these is to verify user entries in edit
fields, as part of a "bullet-proof" data entry mechanism. Normally one does
not want to allow hex numbers here, and two routines are enough:
IsNumericInteger and IsNumeric as in this thread (i.e. allowing floating
point).


A related task (challenge #1) of some interest is to implement a key filter
that prevents bad key-strokes:

local fn CouldBeNumeric( s as str255 )
// Return _zTrue if s is floating-point-numeric or could become
// so by appending further characters, otherwise return _false
dim couldBe as boolean
//...your challenge #1 code goes here...
couldBe = _zTrue // or _false
end fn = couldBe

local fn HandleEdit
long if fn CouldBeNumeric( edit$( 1 ) + tekey$ )
tekey$ = tekey$ // accept key-stroke
xelse
beep // reject
end if
end fn

window 1
edit menu 2
on edit fn HandleEdit
text _sysFont, 12
edit field 1, "", (10,20)-(150,38)
do
handleevents
until 0


The remaining task (challenge #2) is to prevent the user from pasting
non-numeric rubbish into the edit field.

Robert P.