List, I just wanted to point out another solution from Bill Thoen that seems to be pretty bulletproof:
function IsNumeric (byval sVal as string) as logical dim i as integer For i = 1 to Len(sVal) If NOT InStr (1, " 0123456789.+-e", mid$(sVal,i,1)) Then 'String contains a non-numeric character! Exit Function End If Next IsNumeric = 1 end function It basically steps through the string that's being evaluated, testing each character against a list of 'legal' numeric characters, and if it finds one that's 'illegal', exits the function and returns FALSE. Intuitively you would expect that this approach would be much slower, but in practical usage it blazes along just as fast as the str$(val()) method. Tim Nuteson Target -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 18, 2004 9:43 AM To: Spencer Simpson Cc: [EMAIL PROTECTED] Subject: RE: MI-L Testing to ensure a string is numeric Spencer, In terms of speed, I would mostly stick with just the val function. Just the idea that converting a string to a number and back to a string and comparing them is fraught with danger, at least for floating point. Note that the val function returns a number. It returns 0 for things that don't parse and for real zeroes. So if you had code like: num = val(somestring) if num <> 0 then ' this is definitely a number ' return true else ' parse the string looking for spaces/tabs and then a 0 digit to determine a true 0 endif This code will be much faster if you think that you usually do have numbers. The val function will do all the work for you. Call it an optimistic implementation. You can even design the function so it returns the number on success so that you don't bother doing it again. Eric Blasenheim Software Architect MapInfo Corporation Mail List: [EMAIL PROTECTED] From: on 05/17/2004 11:54 AM AST To: <[EMAIL PROTECTED]> cc: Subject: RE: MI-L Testing to ensure a string is numeric No, it will notThere are a couple of ways to implement them, some of which are more efficient than others. The str$(val()) method is the fastest method, but it's not the most robust. For one thing, it works only for integers. However, it's great for quick applications that require only integers. For robust, idiot-proof applications, you need more sophisticated validation routines. It is possible, of course, to write a routine that scans the string for the correct format, but this can be very slow, given that you have to make calls to mid$() for every character in the string. If you've been programming for any length of time, you've probably written one that you can adapt to MapBasic. Or you can write one in a faster language, put it in a DLL, and link to it from MapBasic (scanf is NOT recommended). Another method is to try assigning the string to a MapBasic window variable, and catching any errors. This method is optimal for validating strings that can take non-integer values. run command "Dim v_dbl as float" ... function good_float (ByVal s as string, f as float) as logical On Error Goto notvalid run command "v_dbl="+sval OnError goto 0 f=val(Sval) good_float = true exit function notvalid: resume failure failure: good_float = false end function Hope this helps Spencer -----Original Message----- From: B. Thoen [mailto:[EMAIL PROTECTED] Sent: Monday, May 17, 2004 11:05 AM To: Tim.Nuteson Cc: [EMAIL PROTECTED] Subject: Re: SUM: MI-L Testing to ensure a string is numeric Would that algorithm return the correct result if you fed it a numeric string like '0023456', or are numbers with leading zeros not going to be encountered? On Mon, 17 May 2004, Tim.Nuteson wrote: > Thanks to all who responded to my question: How can I ensure that a > value entered into an EditText control of a MB dialog is numeric? The > simplest solution was offered by Michael Taylor, Martin Highham, and > Robert Crossley: > > If str$(val(teststring)) = teststring then > 'numeric > Else > 'not > End If > > Thanks again, > > Tim Nuteson > Target > > > --------------------------------------------------------------------- > List hosting provided by Directions Magazine | www.directionsmag.com | > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: > [EMAIL PROTECTED] > Message number: 11784 > --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 11788 --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 11789 --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 11816 --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 11823