Value greater than value issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Acrobatic

    Value greater than value issue

    Hello,

    I know this will be an easy fix--but as of now I'm banging my head
    against the wall. I need a fresh perspective from the group to see
    what my problem is:

    This is a simple accounting application, and the code below is
    checking to see if a user's withdraw request is greater than their
    available balance:


    $withdraw_reque st = $_REQUEST['withdraw_amoun t']; // 543.21
    $withdraw_maxim um = $user['available_bala nce']; // 543.21

    if($withdraw_re quest $withdraw_maxim um) {
    echo "Insufficie nt funds.";
    } else {
    echo "Processing..." ;
    }


    In my application, both values are equal (543.21), but
    $withdraw_reque st $widthdraw_maxi mum still evaluates to TRUE, and
    thus shows "Insufficie nt funds." If the two values are whole numbers,
    like "543", then they evaluate the way I expect them to.

    I've tried everything I can think of, like

    $withdraw_reque st = floatval($_REQU EST['withdraw_amoun t']);
    $withdraw_maxim um = floatval($user['available_bala nce']);

    as well as doing an "isnumeric" check on both values (they both return
    true), but still no luck. If $withdraw_reque st is less than (<) the
    $withdraw_maxim um--ie 543.20 < 543.21, the script works fine.

    Also, if I hard code the values, the script works fine. Somewhere
    between pulling the maximum from the database and getting the
    $_REQUEST variable things are getting lost in translation.

    Thanks for any advice
  • Good Man

    #2
    Re: Value greater than value issue

    Acrobatic <jbnunn@gmail.c omwrote in news:7bc09fbf-5b53-4d5d-bfc7-
    326fee3c2ba9@d2 1g2000prf.googl egroups.com:

    Also, if I hard code the values, the script works fine. Somewhere
    between pulling the maximum from the database and getting the
    $_REQUEST variable things are getting lost in translation.
    >
    Thanks for any advice
    >
    my quick suggestion would be to make sure your values really are what you
    think they are... echo out to the screen

    also, note that "floatval" is a function whereas "float" will evaluate a
    string numerically.

    so...

    <?php

    $withdraw_reque st = (float)$_REQUES T['withdraw_amoun t'];
    $withdraw_maxim um = (float)$user['available_bala nce'];

    echo "comparing $withdraw_reque st and $withdraw_maxim um <br>";

    if($withdraw_re quest $withdraw_maxim um) {
    echo "Insufficie nt funds.";
    } else {
    echo "Processing..." ;
    }

    ?>

    Comment

    • Jerry Stuckle

      #3
      Re: Value greater than value issue

      Acrobatic wrote:
      Hello,
      >
      I know this will be an easy fix--but as of now I'm banging my head
      against the wall. I need a fresh perspective from the group to see
      what my problem is:
      >
      This is a simple accounting application, and the code below is
      checking to see if a user's withdraw request is greater than their
      available balance:
      >
      >
      $withdraw_reque st = $_REQUEST['withdraw_amoun t']; // 543.21
      $withdraw_maxim um = $user['available_bala nce']; // 543.21
      >
      if($withdraw_re quest $withdraw_maxim um) {
      echo "Insufficie nt funds.";
      } else {
      echo "Processing..." ;
      }
      >
      >
      In my application, both values are equal (543.21), but
      $withdraw_reque st $widthdraw_maxi mum still evaluates to TRUE, and
      thus shows "Insufficie nt funds." If the two values are whole numbers,
      like "543", then they evaluate the way I expect them to.
      >
      I've tried everything I can think of, like
      >
      $withdraw_reque st = floatval($_REQU EST['withdraw_amoun t']);
      $withdraw_maxim um = floatval($user['available_bala nce']);
      >
      as well as doing an "isnumeric" check on both values (they both return
      true), but still no luck. If $withdraw_reque st is less than (<) the
      $withdraw_maxim um--ie 543.20 < 543.21, the script works fine.
      >
      Also, if I hard code the values, the script works fine. Somewhere
      between pulling the maximum from the database and getting the
      $_REQUEST variable things are getting lost in translation.
      >
      Thanks for any advice
      >
      Welcome to the world of floating point numbers - where values are not
      generally exact.

      See http://www.php.net/manual/en/language.types.float.php for a
      discussion on it.

      For this particular instance, you should be able to solve the problem with:

      $val1=(intval)( (floatval($_REQ UEST['withdraw_amoun t']) + 0.005)* 100);

      This gets the float value, adds 0.005 to it then multiplies by 100. It
      then gets the integer value of the result. Repeat for the available
      balance.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Acrobatic

        #4
        Re: Value greater than value issue

        For this particular instance, you should be able to solve the problem with:
        >
        $val1=(intval)( (floatval($_REQ UEST['withdraw_amoun t']) + 0.005)* 100);
        >
        This gets the float value, adds 0.005 to it then multiplies by 100. It
        then gets the integer value of the result. Repeat for the available
        balance.
        Thanks guys for the advice. Jerry, your workaround did the trick.


        Comment

        • Toby A Inkster

          #5
          Re: Value greater than value issue

          Acrobatic wrote:
          This is a simple accounting application, and the code below is checking
          to see if a user's withdraw request is greater than their available
          balance:
          To build on Jerry's post, floats are a really bad idea for representing
          currencies. They're just too woolly. As PHP doesn't include any fixed-
          precision decimal data type, the best option is to represent money as an
          integer -- using the smallest unit of currency possible (e.g. cents
          rather than euros; pence rather than pounds).

          You then reformat it as a price in the larger unit of currency only when
          it needs to be displayed.

          --
          Toby A Inkster BSc (Hons) ARCS
          [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
          [OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]

          Sharing Music with Apple iTunes

          Comment

          • allain

            #6
            Re: Value greater than value issue

            On Dec 8, 5:51 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
            wrote:
            Acrobatic wrote:
            This is a simple accounting application, and the code below is checking
            to see if a user's withdraw request is greater than their available
            balance:
            >
            To build on Jerry's post, floats are a really bad idea for representing
            currencies. They're just too woolly. As PHP doesn't include any fixed-
            precision decimal data type, the best option is to represent money as an
            integer -- using the smallest unit of currency possible (e.g. cents
            rather than euros; pence rather than pounds).
            >
            You then reformat it as a price in the larger unit of currency only when
            it needs to be displayed.
            >
            --
            Toby A Inkster BSc (Hons) ARCS
            [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
            [OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]
            >
            Sharing Music with Apple iTunes
            http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
            Geez, that must have been a doozey bug. :)

            Comment

            • Jerry Stuckle

              #7
              Re: Value greater than value issue

              allain wrote:
              On Dec 8, 5:51 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
              wrote:
              >Acrobatic wrote:
              >>This is a simple accounting application, and the code below is checking
              >>to see if a user's withdraw request is greater than their available
              >>balance:
              >To build on Jerry's post, floats are a really bad idea for representing
              >currencies. They're just too woolly. As PHP doesn't include any fixed-
              >precision decimal data type, the best option is to represent money as an
              >integer -- using the smallest unit of currency possible (e.g. cents
              >rather than euros; pence rather than pounds).
              >>
              >You then reformat it as a price in the larger unit of currency only when
              >it needs to be displayed.
              >>
              >--
              >Toby A Inkster BSc (Hons) ARCS
              >[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
              >[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]
              >>
              > Sharing Music with Apple iTunes
              > http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
              >
              Geez, that must have been a doozey bug. :)
              >
              Not at all. A very common bug for anyone who's worked with floating
              point values for more than six months or so.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              Working...