multiplication math not working ?

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

    multiplication math not working ?

    hi group -

    I had this in a function - so I've just moved this out of the function
    for readabilty.

    Can someone please tell me WHY does this not return the result of :
    n1 times n2

    As I said - I took it out of the function just for readability :
    In my php file :

    $number_percent ="25";
    echo "history['order_total'] is ".$history['order_total']."<br />";
    echo "number_per cent is ".$number_perce nt."<br />";
    $total=(($histo ry['order_total'])*$number_perce nt);
    echo "total is ".$total."< br />";

    And here is the output in the browser :
    history['order_total'] is $241.47
    number_percent is 25
    total is 0

    WHY ? Any advice or pointers would be much appreciated!

  • Rik

    #2
    Re: multiplication math not working ?

    One <david.hunter@g mail.comwrote:
    hi group -
    >
    I had this in a function - so I've just moved this out of the function
    for readabilty.
    >
    Can someone please tell me WHY does this not return the result of :
    n1 times n2
    >
    As I said - I took it out of the function just for readability :
    In my php file :
    >
    $number_percent ="25";
    echo "history['order_total'] is ".$history['order_total']."<br />";
    echo "number_per cent is ".$number_perce nt."<br />";
    $total=(($histo ry['order_total'])*$number_perce nt);
    echo "total is ".$total."< br />";
    >
    And here is the output in the browser :
    history['order_total'] is $241.47
    number_percent is 25
    total is 0
    >
    WHY ? Any advice or pointers would be much appreciated!
    Because '$241.47' is not a number, it's a string representation of a
    currency. PHP doesn't know anything about that, 'currency' is not a type..
    When performing math on it, PHP will try to make a number out of it, and
    encounters '$' as the first character. This is not something it
    recognizes, and it will default to 0. And 0 * 25 = 0.

    You might want to remove the currency symbol from the string, and just add
    it back when you have to echo/print the total.

    Also, I'd think if $number_percent = 25, you might want $total =
    $history['order_total'] * (1+($number_per cent/100));
    --
    Rik Wasmus

    Comment

    • Jerry Stuckle

      #3
      Re: multiplication math not working ?

      One wrote:
      hi group -
      >
      I had this in a function - so I've just moved this out of the function
      for readabilty.
      >
      Can someone please tell me WHY does this not return the result of :
      n1 times n2
      >
      As I said - I took it out of the function just for readability :
      In my php file :
      >
      $number_percent ="25";
      echo "history['order_total'] is ".$history['order_total']."<br />";
      echo "number_per cent is ".$number_perce nt."<br />";
      $total=(($histo ry['order_total'])*$number_perce nt);
      echo "total is ".$total."< br />";
      >
      And here is the output in the browser :
      history['order_total'] is $241.47
      number_percent is 25
      total is 0
      >
      WHY ? Any advice or pointers would be much appreciated!
      >
      $number_percent is a string containing 25, not the numeric value 25.
      When you use a string in a calculation it's numeric value is zero.

      Try
      $number_percent =25;

      instead (and ensure that $history['order_total'] is also numeric).


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

      Comment

      • One

        #4
        Re: multiplication math not working ?

        Because '$241.47' is not a number, it's a string representation of a
        currency. PHP doesn't know anything about that, 'currency' is not a type.
        When performing math on it, PHP will try to make a number out of it, and
        encounters '$' as the first character. This is not something it
        recognizes, and it will default to 0. And 0 * 25 = 0.
        >
        You might want to remove the currency symbol from the string, and just add
        it back when you have to echo/print the total.
        >
        Also, I'd think if $number_percent = 25, you might want $total =
        $history['order_total'] * (1+($number_per cent/100));
        Hi Rik - thank you the reply.

        I used string replace to remove the dollar sign, then I simply
        multiplied the two numbers, and it is still calculating as zero.
        Can I cast this variable to an int ??

        Input :
        $number_percent ="25";
        echo "history['order_total'] is ".$history['order_total']."<br />";
        $removeit=str_r eplace('$' ,'', $history['order_total']);
        echo "strip the $ sign ".$removeit."<b r />";
        $total = ($removeit * (1+($number_per cent/100)));
        echo "<br />total is ".$total."< br />";

        Output :
        history['order_total'] is $241.47
        strip the $ sign 241.47
        total is 0


        :-(




        Comment

        • One

          #5
          Re: multiplication math not working ?

          On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          $number_percent is a string containing 25, not the numeric value 25.
          When you use a string in a calculation it's numeric value is zero.
          >
          Try
          $number_percent =25;
          >
          instead (and ensure that $history['order_total'] is also numeric).
          Hi Jerry - thanks much for trying.
          Same result :
          Input :
          $number_percent =25;
          echo "history['order_total'] is ".$history['order_total']."<br />";
          $removeit=str_r eplace('$' ,'', $history['order_total']);
          echo "strip the $ sign ".$removeit."<b r />";
          $total = ($removeit * $number_percent );
          echo "<br />total is ".$total."< br />";

          Output :
          history['order_total'] is $241.47
          strip the $ sign 241.47
          total is 0

          This is bonkers - I'm *sure* I have done this type of thing in the
          past without issue. :-(


          Comment

          • Rik

            #6
            Re: multiplication math not working ?

            One <david.hunter@g mail.comwrote:
            On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            >$number_percen t is a string containing 25, not the numeric value 25.
            >When you use a string in a calculation it's numeric value is zero.
            >>
            >Try
            > $number_percent =25;
            >>
            >instead (and ensure that $history['order_total'] is also numeric).
            >
            Hi Jerry - thanks much for trying.
            Same result :
            Input :
            $number_percent =25;
            echo "history['order_total'] is ".$history['order_total']."<br />";
            $removeit=str_r eplace('$' ,'', $history['order_total']);
            echo "strip the $ sign ".$removeit."<b r />";
            $total = ($removeit * $number_percent );
            echo "<br />total is ".$total."< br />";
            >
            Output :
            history['order_total'] is $241.47
            strip the $ sign 241.47
            total is 0
            >
            This is bonkers - I'm *sure* I have done this type of thing in the
            past without issue. :-(
            This works here:
            <?php
            $history['order_total'] = '$241.47';
            $number_percent ="25";//should be without the quotes indeed, but just to
            show PHP can cast this correctly
            echo "history['order_total'] is ".$history['order_total']."<br />";
            $removeit=str_r eplace('$' ,'', $history['order_total']);
            echo "strip the $ sign ".$removeit."<b r />";
            $total = ($removeit * (1+($number_per cent/100)));
            echo "<br />total is ".$total."< br />";
            ?>

            So I'd say there's something wrong with the exact contents of
            $history['order_total'] in your script.

            What if you do this (a bit overkill):

            $removeit = preg_replace('/[^0-9.]/','',$history['order_total']);

            If this doesn't work, I'm very curious what the output of
            floatval($histo ry['order_total'])(or floatval($remov eit)) is....
            --
            Rik Wasmus

            Comment

            • Jerry Stuckle

              #7
              Re: multiplication math not working ?

              One wrote:
              On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
              >$number_percen t is a string containing 25, not the numeric value 25.
              >When you use a string in a calculation it's numeric value is zero.
              >>
              >Try
              > $number_percent =25;
              >>
              >instead (and ensure that $history['order_total'] is also numeric).
              >
              Hi Jerry - thanks much for trying.
              Same result :
              Input :
              $number_percent =25;
              echo "history['order_total'] is ".$history['order_total']."<br />";
              $removeit=str_r eplace('$' ,'', $history['order_total']);
              echo "strip the $ sign ".$removeit."<b r />";
              $total = ($removeit * $number_percent );
              echo "<br />total is ".$total."< br />";
              >
              Output :
              history['order_total'] is $241.47
              strip the $ sign 241.47
              total is 0
              >
              This is bonkers - I'm *sure* I have done this type of thing in the
              past without issue. :-(
              >
              >
              Rik caught one and I caught the other (I missed the dollar sign).

              Both are string in your setup. Use floatval() to convert your
              $history['order_total'] to a floating point number.

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

              Comment

              • Jerry Stuckle

                #8
                Re: multiplication math not working ?

                One wrote:
                On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                >$number_percen t is a string containing 25, not the numeric value 25.
                >When you use a string in a calculation it's numeric value is zero.
                >>
                >Try
                > $number_percent =25;
                >>
                >instead (and ensure that $history['order_total'] is also numeric).
                >
                Hi Jerry - thanks much for trying.
                Same result :
                Input :
                $number_percent =25;
                echo "history['order_total'] is ".$history['order_total']."<br />";
                $removeit=str_r eplace('$' ,'', $history['order_total']);
                echo "strip the $ sign ".$removeit."<b r />";
                $total = ($removeit * $number_percent );
                echo "<br />total is ".$total."< br />";
                >
                Output :
                history['order_total'] is $241.47
                strip the $ sign 241.47
                total is 0
                >
                This is bonkers - I'm *sure* I have done this type of thing in the
                past without issue. :-(
                >
                >
                Better yet - store the data as a floating point value without the dollar
                sign. Then insert the dollar sign when you're ready to print.

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

                Comment

                • Rik

                  #9
                  Re: multiplication math not working ?

                  Jerry Stuckle <jstucklex@attg lobal.netwrote:
                  Better yet - store the data as a floating point value without the dollar
                  sign. Then insert the dollar sign when you're ready to print.
                  Indeed, check money_format().
                  --
                  Rik Wasmus

                  Comment

                  • One

                    #10
                    Re: multiplication math not working ?

                    On Feb 7, 1:00 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    One wrote:
                    On Feb 7, 11:34 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    $number_percent is a string containing 25, not the numeric value 25.
                    When you use a string in a calculation it's numeric value is zero.
                    >
                    Try
                    $number_percent =25;
                    >
                    instead (and ensure that $history['order_total'] is also numeric).
                    >
                    Hi Jerry - thanks much for trying.
                    Same result :
                    Input :
                    $number_percent =25;
                    echo "history['order_total'] is ".$history['order_total']."<br />";
                    $removeit=str_r eplace('$' ,'', $history['order_total']);
                    echo "strip the $ sign ".$removeit."<b r />";
                    $total = ($removeit * $number_percent );
                    echo "<br />total is ".$total."< br />";
                    >
                    Output :
                    history['order_total'] is $241.47
                    strip the $ sign 241.47
                    total is 0
                    >
                    This is bonkers - I'm *sure* I have done this type of thing in the
                    past without issue. :-(
                    >
                    Rik caught one and I caught the other (I missed the dollar sign).
                    >
                    Both are string in your setup. Use floatval() to convert your
                    $history['order_total'] to a floating point number.
                    Hi guys - thanks SO much for your replies - it is still not working -
                    I'm using the floatval properly but it is returning a zero :

                    Input :
                    echo "history['order_total'] is ".$history['order_total']."<br />";
                    $removeit=str_r eplace('$' ,'', $history['order_total']);
                    echo $removeit;
                    $floatandremove = floatval($remov eit);
                    echo "<br />floatandremo ve is ".$floatandremo ve;
                    exit;

                    Output :
                    history['order_total'] is $241.47
                    241.47
                    floatandremove is 0

                    This is getting crazy!

                    Comment

                    • Rik

                      #11
                      Re: multiplication math not working ?

                      One <david.hunter@g mail.comwrote:
                      Input :
                      echo "history['order_total'] is ".$history['order_total']."<br />";
                      $removeit=str_r eplace('$' ,'', $history['order_total']);
                      echo $removeit;
                      $floatandremove = floatval($remov eit);
                      echo "<br />floatandremo ve is ".$floatandremo ve;
                      exit;
                      >
                      Output :
                      history['order_total'] is $241.47
                      241.47
                      floatandremove is 0
                      >
                      There must be something else going on with your currency string.
                      Please use this on that string, and report it's results back to us:

                      echo preg_replace('/(.)/se','"[".ord("$1") ."]"',$history['order_total']);
                      --
                      Rik Wasmus

                      Comment

                      • One

                        #12
                        Re: multiplication math not working ?

                        On Feb 8, 2:01 pm, Rik <luiheidsgoe... @hotmail.comwro te:
                        One <david.hun...@g mail.comwrote:
                        Input :
                        echo "history['order_total'] is ".$history['order_total']."<br />";
                        $removeit=str_r eplace('$' ,'', $history['order_total']);
                        echo $removeit;
                        $floatandremove = floatval($remov eit);
                        echo "<br />floatandremo ve is ".$floatandremo ve;
                        exit;
                        >
                        Output :
                        history['order_total'] is $241.47
                        241.47
                        floatandremove is 0
                        >
                        There must be something else going on with your currency string.
                        Please use this on that string, and report it's results back to us:
                        >
                        echo preg_replace('/(.)/se','"[".ord("$1") ."]"',$history['order_total']);
                        --
                        Rik Wasmus
                        Thanks again Rik - here you go :

                        Input :
                        echo "history['order_total'] is ".$history['order_total']."<br />";
                        echo preg_replace('/(.)/se','"[".ord("$1") ."]"',
                        $history['order_total']);
                        exit;

                        Output :
                        history['order_total'] is $241.47
                        [60][98][62][36][50][52][49][46][52][55][60][47][98][62]

                        Comment

                        • Toby A Inkster

                          #13
                          Re: multiplication math not working ?

                          One wrote:
                          history['order_total'] is $241.47
                          [60][98][62][36][50][52][49][46][52][55][60][47][98][62]
                          $history['order_total'] contains '<b>$241.47</b>'. This is not a number.

                          $history['order_total'] = (float)preg_rep lace('/[^0-9\.]/', '',
                          strip_tags($his tory['order_total']));

                          --
                          Toby A Inkster BSc (Hons) ARCS
                          Contact Me ~ http://tobyinkster.co.uk/contact
                          Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

                          * = I'm getting there!

                          Comment

                          • One

                            #14
                            Re: multiplication math not working ?

                            On Feb 9, 5:51 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
                            wrote:
                            One wrote:
                            history['order_total'] is $241.47
                            [60][98][62][36][50][52][49][46][52][55][60][47][98][62]
                            >
                            $history['order_total'] contains '<b>$241.47</b>'. This is not a number.
                            >
                            $history['order_total'] = (float)preg_rep lace('/[^0-9\.]/', '',
                            strip_tags($his tory['order_total']));

                            Mr. Toby!
                            You da man - thanks very much!

                            Thank you also to Rik and Jerry for taking the time to resolve this
                            issue.


                            Comment

                            Working...