-----Original Message-----
From: Spencer Simpson [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 17, 2004 2:06 PM
To: 'Soporte'
Subject: RE: MI-L Testing to ensure a string is numeric (An unortodox way)

I don't know which version of MapBasic you're using. 

In the version I use (5.0) this produces an error
("Numeric values required") no matter what's in the string.

It's essentially a type mismatch error.

Spencer



-----Original Message-----
From: Soporte [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 17, 2004 2:02 PM
To: 'MapInfo-L'
Subject: Re: MI-L Testing to ensure a string is numeric (An unortodox way)

Why don't you try something like this:

Declare Sub Main
    Sub Main()
        Dim a as string
        OnError Goto LocalError
        a = a / 1
        Exit sub

    LocalError:
        If Err() = 306 Then
        Note "a is not numeric"
    End If
End Sub


----- Original Message ----- 
From: "Spencer Simpson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>; "'Spencer Simpson'" <[EMAIL PROTECTED]>
Cc: "'MapInfo-L'" <[EMAIL PROTECTED]>
Sent: Monday, May 17, 2004 1:47 PM
Subject: RE: MI-L Testing to ensure a string is numeric


I'm sorry, but Str$(val())is NOT reliable for testing floating-point values.

I've been burned by this many, many times, and only use str$(val()) to test
integers in very simple applications.


In addition to the issue of leading zeroes, there are also issues with:

1. Looking for a "-" sign at the beginning, for a negative number (-00001.2
should be considered valid)

2. getting rid of trailing zeroes, but only if there's a decimal in the
number. (123.4500 must match 123.45, but 1234500 must match 12345)
DeformatNumber$() will change a comma used by Europeans into a decimal point
for Val() to use.

3. Trailing decimal point for integers. ("123." must match "123")

4. Settings which limit the number of digits after the decimal point.  
On my system, str$(val("12345.678")) returns "12345.68".   

This is probably the most fatal issue, as the application writer has no
control over it (it's controlled by the user's local settings).  'Set Format
Number "9,999.9"' does NOT override it.

There are so many special cases that it's better to discard the 
entire system and use one of the holistic approaches I described in 
my earlier posting.

Hope this helps
Spencer


-----Original Message-----
From: Martin Higham [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 17, 2004 12:12 PM
To: Spencer Simpson
Cc: MapInfo-L
Subject: RE: MI-L Testing to ensure a string is numeric

No - str$(val("1.2345")) returns "1.2345".

Where it may fall down, is in the use of thousands separators.  For this you
should use
str$(val(DeformatNumber$("10,001"))).

With this, it will be robust, but you may want to check for and strip
leading zeroes (as Bill highlights).

Best Regards,

Martin Higham
Avantra Geosystems

ph (61 3) 8504 0428   0425-730-428
fx (61 3) 9596 7997
www.avantra.com.au




> -----Original Message-----
> From: Spencer Simpson [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, 18 May 2004 01:55
> To: [EMAIL PROTECTED]
> 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: 11795



---------------------------------------------------------------------
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Message number: 11797

Reply via email to