Results 1 to 6 of 6

Thread: Catching improper StrToFloat values

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Catching improper StrToFloat values

    For some odd reason I cannot seem to recall a proper way to catch improper values fed to StrToFloat().

    I've even used try .. except, but it doesn't seem to want to catch it. :?

    Anyone know a nice way to do this?

    I guess I'm used to Delphi where the TEdit components can be set to automatically block improper numerical characters. 0-9, '-' and '.'
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2

    Catching improper StrToFloat values

    Best thing to use StrToFloatDef function (Like StrToIntDef) which catches the EConvertError and returns the default value. I checked and FreePascal has this function
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  3. #3

    Catching improper StrToFloat values

    There's possibly a function for that, until then, you could write your own.
    Something like:
    Code:
    function checkValue&#40;text&#58;string&#41;&#58;boolean;
    var i &#58; byte;
    begin
      result &#58;= false;
      for i &#58;= 1 to length&#40;Text&#41; do
      begin
        if not &#40;text&#91;i&#93; in &#91;'0'..'9','.'&#93;&#41; then
        begin
          result &#58;= true;
          exit;
        end;
      end;
    end;
    
    procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
    begin
       if checkValue&#40;edit1.text&#41; then showmessage&#40;'incorrect value'&#41;;
    end;

  4. #4

    Catching improper StrToFloat values

    Two posts at (almost?) the exact same time

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Catching improper StrToFloat values

    :shock: *whew* that was fast, thanks guys!

    Hmm... considered both solutions. (At least both approaches to the solution.)

    Traveler, yours is a bit more involved, but I'd really only consider it unless I could not find something more elegant, like StrToFloatDef. :lol:

    I'll give that a go and if I don't like whats involved then I guess I go with the brute force method approach.

    Thanks again guys!


    BTW, am I right in assuming that this is a non-issue in Delphi's components? I remember having to deal with this before, but it was never quite so tricky to resolve.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Catching improper StrToFloat values

    Also another common thing that always happens:

    If you are using a Try, Except loop in Delphi, and it still breaks on the error, just turn off Stop on Delphi Exceptions in the Debugger options page.
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •