Calculation script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrking
    New Member
    • Feb 2008
    • 28

    #1

    Calculation script

    I am looking to make a script that does a calculation for me.

    The user is to enter two variables, height (H) and width (W), click "calculate" and the value will appear, hopefully on the same page.

    The formula will be (((H*W)+150)*1. 65) (pretty easy)

    However, I would like it to round up to the nearest 10th. So if the value comes out to 411 I would like it to display 420. If the solution is 579 it will round up to 580.

    Sounds easy but I am not sure where to start.

    Any guidance will be appreciated.

    Thanks!!!!
  • mrking
    New Member
    • Feb 2008
    • 28

    #2
    This is my scripts so far:

    [PHP]
    <?PHP
    $cost = ((($_REQUEST["height"]*$_REQUEST["width"])+150)*1.65);
    echo "The cost of the commission will be " .$cost;
    ?>
    [/PHP]

    Code:
    <form action="calculate.php" method="get">
    <td align="left">
    Height:<input type="text" size="4" name="height">
    </select>
    </td>
    <td align="left">
    Width:<input type="text" size="4" name="width">
    </select>
    </td>
    
    
    
    <td colspan="2" align="center">
    <input type="submit" value="Calculate">
    This works now, but how do I get it to round up to the nearest 10 and how do I get it to display on the same page after I hit "Calculate" ?

    Thanks!!!

    PS> FWIW, My site is built with WordPress

    Comment

    • mrking
      New Member
      • Feb 2008
      • 28

      #3
      i've tried this:

      [PHP]
      <?

      $cost=((($_REQU EST["height"]*$_REQUEST["width"])+150)*1.65);

      echo "The cost of the commission will be " .round($cost);

      ?>
      [/PHP]


      That just rounds it up to a number with no decimals. Any advice on how to round it up to the nearest 10th?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        [php]
        <?php
        function t_round($num) {
        return ($num % 10 >= 5 ? ceil($num / 10)*10 : floor($num / 10)*10);
        }
        print t_round(4);
        ?>
        [/php]
        There is no specified function for doing this, but i found that ^ after some lurking.
        Quite simple, actually..
        If you don't know what's going on, check out the ceil() and floor() functions.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Then that defeats the point of this forum, doesn't it?
          Last edited by ronverdonk; Mar 6 '08, 07:26 PM. Reason: removed spam

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by markusn00b
            Then that defeats the point of this forum, doesn't it?
            Sorry I interrupted, but there was a spammer hanging around all forums. That is why I removed hist posts and he was banned for life.

            moderator

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by ronverdonk
              Sorry I interrupted, but there was a spammer hanging around all forums. That is why I removed hist posts and he was banned for life.

              moderator
              Yeh, i noticed.
              I was going to PM you but you werent online

              Comment

              • dlite922
                Recognized Expert Top Contributor
                • Dec 2007
                • 1586

                #8
                Originally posted by mrking
                I am looking to make a script that does a calculation for me.

                The user is to enter two variables, height (H) and width (W), click "calculate" and the value will appear, hopefully on the same page.

                The formula will be (((H*W)+150)*1. 65) (pretty easy)

                However, I would like it to round up to the nearest 10th. So if the value comes out to 411 I would like it to display 420. If the solution is 579 it will round up to 580.

                Sounds easy but I am not sure where to start.

                Any guidance will be appreciated.

                Thanks!!!!

                It IS possible to do this the old fashion way.

                Lets say you start off with 514 and you want to end up with 520

                What you do is divide by 10:
                51.4

                do the round and add one
                round(51.4) will give you 51
                add one will give you 52

                then multiply by ten!

                that will give you a round up to 10.

                if you want to round to 100, just divide and multiply by 100.

                The other thing to consider is what if you get 500, do you want 510?

                The way you can check for this is to divide by 100 and see if you get an integer number. 500/100 = 5 (hint: check for float() or presence of "." (decimal) in it.

                514/100 will of course give you 5.14.


                Hope that helps, good luck.

                Comment

                • mrking
                  New Member
                  • Feb 2008
                  • 28

                  #9
                  Perfect! Thanks everyone!!!

                  Comment

                  Working...