Round and Ceil problem

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

    Round and Ceil problem

    I have a problem that I can't seem to solve. I have checked the round,
    ceil and floor functions and they don't seem to do what I want.

    I have an entry like this

    <?php
    $tax = .065;
    $ad2day = 104.50;

    $ad2day = round(($ad2day * $tax)+ $ad2day, 2);
    echo printf ("%.2f", $ad2day);
    ?>

    That outputs $111.29, the result of multiplying 104.50x.065 with the
    answer rounded to 2 decimal places, then adding in the 104.50 of the
    $ad1day variable.

    What I want to do is multiply the $ad2day variable by the $tax and come
    up with a result that will round up any remainder past two decimal
    places to the next decmial place. In the example above, the ($ad2day *
    $tax) comes out to 6.7925. I want to round that up to 2 decimal places
    to 6.80 if there is *any* remainder past the two digits. So 6.2401 would
    be 6.25, 6.2499 would be 6.25, etc.

    Round doesn't seem to do it and ceil only works (i think anyway)
    rounding up to whole numbers. What do I need to do to get the result the
    way I want it?

    Thanks
  • Pedro Graca

    #2
    Re: Round and Ceil problem

    Jack wrote:[color=blue]
    > I have checked the round,
    > ceil and floor functions and they don't seem to do what I want.[/color]
    [...][color=blue]
    > So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.
    >
    > What do I need to do to get the result the
    > way I want it?[/color]

    $x = 6.2501;
    $y = ceil($x * 100) / 100; // 6.26

    --
    Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
    == ** ## !! !! ## ** ==
    TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
    bypass the spam filter. I will answer all pertinent mails from a valid address.

    Comment

    • JackM

      #3
      Re: Round and Ceil problem

      Pedro Graca wrote:[color=blue]
      > Jack wrote:
      >
      > [...]
      >[color=green]
      >>So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.
      >>
      >>What do I need to do to get the result the
      >>way I want it?[/color]
      >
      >
      > $x = 6.2501;
      > $y = ceil($x * 100) / 100; // 6.26
      >[/color]
      Okay thanks, but that assumes that I already know the value of $x when
      in fact I won't. How can I incorporate that into my original statement of
      $ad2day = round(($ad2day * $tax)+ $ad2day, 2);

      I really don't want to write a function for each of my 150 entries.

      Comment

      • Pedro Graca

        #4
        Re: Round and Ceil problem

        JackM wrote:[color=blue]
        > Pedro Graca wrote:[color=green]
        >> Jack wrote:
        >>
        >> [...]
        >>[color=darkred]
        >>>So 6.2401 would be 6.25, 6.2499 would be 6.25, etc.
        >>>
        >>>What do I need to do to get the result the
        >>>way I want it?[/color]
        >>
        >>
        >> $x = 6.2501;
        >> $y = ceil($x * 100) / 100; // 6.26
        >>[/color]
        > How can I incorporate that into my original statement of
        > $ad2day = round(($ad2day * $tax)+ $ad2day, 2);[/color]



        $x = $ad2day * $tax;
        $y = ceil($x * 100) / 100;
        $ad2day = round($y + $ad2day, 2);


        Replacing $y:

        $x = $ad2day * $tax;
        $ad2day = round((ceil($x * 100) / 100) + $ad2day, 2);
        /* $y = ceil($x * 100) / 100; */

        Replacing $x

        $ad2day = round((ceil(($a d2day * $tax) * 100) / 100) + $ad2day, 2);
        /* $x = $ad2day * $tax; */

        --
        Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
        == ** ## !! !! ## ** ==
        TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
        bypass the spam filter. I will answer all pertinent mails from a valid address.

        Comment

        • JackM

          #5
          Re: Round and Ceil problem

          Pedro Graca wrote:[color=blue]
          > JackM wrote:
          >[color=green]
          >>Pedro Graca wrote:
          >>[color=darkred]
          >>>
          >>>
          >>>$x = 6.2501;
          >>>$y = ceil($x * 100) / 100; // 6.26
          >>>[/color]
          >>
          >>How can I incorporate that into my original statement of
          >>$ad2day = round(($ad2day * $tax)+ $ad2day, 2);[/color]
          >
          >
          > $x = $ad2day * $tax;
          > $y = ceil($x * 100) / 100;
          > $ad2day = round($y + $ad2day, 2);
          >
          >
          > Replacing $y:
          >
          > $x = $ad2day * $tax;
          > $ad2day = round((ceil($x * 100) / 100) + $ad2day, 2);
          > /* $y = ceil($x * 100) / 100; */
          >
          > Replacing $x
          >
          > $ad2day = round((ceil(($a d2day * $tax) * 100) / 100) + $ad2day, 2);
          > /* $x = $ad2day * $tax; */
          >[/color]

          Thanks Pedro, the replacing $x option is the one I was seeking. Works
          like a charm. Much obliged for the help.

          Comment

          Working...