Quaterly rounding in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexphd
    New Member
    • Dec 2006
    • 19

    Quaterly rounding in PHP

    I had post a thread on rounding before, but it really wasnt what I needed. I need quaterly rounding I have tried this

    $FormatFinalTot al = (ceil($FinalTot al*20))/20;

    and that rounds 67.67 to 67.70 or 23.23 to 23.25


    what i need is 60.10 to 60.25 or 67.67 to 67.75

    or 67.80 to 68.00



    Any help would greatly be appreciated.

    Thanks,

    Alex
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    Try this:

    Code:
    $FormatFinalTotal = (round($FinalTotal*4))/4;
    Its all just mathematics.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by coolsti
      Try this:

      Code:
      $FormatFinalTotal = (round($FinalTotal*4))/4;
      Its all just mathematics.
      You missed one thing though. It's ceil, not round.

      And alexphd, you could have continued in the same thread rather then a new thread.

      Comment

      • coolsti
        Contributor
        • Mar 2008
        • 310

        #4
        Hmmm, ceil or round depends on the intentions of the OP. It would be "round" if the OP wants to round the result to the nearest quarter, and "ceil" if the OP wants to round up to the nearest quarter.

        Or?

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Look at his requirements, you will come to know about his intentions..

          60.10 to 60.25 or 67.67 to 67.75 or 67.80 to 68.00

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Originally posted by hsriat
            Look at his requirements, you will come to know about his intentions..

            60.10 to 60.25 or 67.67 to 67.75 or 67.80 to 68.00
            I'm having a horrible case of Deja Vu

            0_0

            -Dan

            Comment

            • alexphd
              New Member
              • Dec 2006
              • 19

              #7
              both answered helped out a lot.

              I have a question though how did you know to divide by 4 and multiply by 4?

              Alex

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Originally posted by alexphd
                both answered helped out a lot.

                I have a question though how did you know to divide by 4 and multiply by 4?

                Alex
                It's an old mathematical trick. I learnt it at uni, but it is quite simple when you think about it! You can do it with normal rounding (use 2 instead of 4) etc...

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Well, I never used this trick. It just came across my mind when someone in JavaScript forum asked me to round a number to the nearest hundredth.
                  ie. 5.867 to 5.87, 3.653 to 3.65

                  There's no second parameter in JavaScript like in PHP which could define the number of digits. So I told him to first multiply it with 100, then round it and then divide it with 100 again.

                  As far as the usage of the multiple (4 or 20) is concerned, inverse your interval, you will get the required multiple.

                  eg. if your interval is 0.05, multiple is 1/0.5 = 20
                  interval = 0.25, multiple = 1/0.25 = 4 (quarterly rounding case)

                  And as far as ceil and round are concerned, ceil will return the upper limit, and round will return the nearest limit, whether upper or lower. floor will return the lower limit always.

                  Code:
                  Number      Ceil        Floor       Round
                  1.78         2           1           2 
                  1.50         2           1           2
                  1.49         2           1           1
                  1.10         2           1           1
                  Regards,
                  Harpreet

                  Comment

                  • coolsti
                    Contributor
                    • Mar 2008
                    • 310

                    #10
                    Hi Alexphd,

                    why did I know to use the factor 4? It just came to me, really. The keyword was that you wanted to round to the nearest "quarter", and in order to use the functions floor(), ceil() or round() you need to remove the "decimal" portion of the number for the whole quarters. And to do this, you need to multiply the number by 4. Then any "quarter", i.e. 0.0, 0.25, 0.5 and 0.75, will become a whole number without decimal. Anything else can then be rounded, rounded up, or rounded down as wished. But then to get back to the original number again you need to reverse the scaling by 4, meaning you need to then divide by 4.

                    Comment

                    Working...