Rounding in php

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

    Rounding in php

    I read the Manuel on php.net for rounding but none of it really applied to me.

    What I am trying to do is round the number 4.23 to 4.25 or 65.76 to 65.80 or 9.98 to 10.00

    Any help would be greatly appreciated.

    Thanks in advance,

    Alex
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You didn't look hard enough, my friend!

    [php]
    echo round(1.34, 2);
    /* the second parameter is to what decimal place the number shall be rounded.
    */
    [/php]

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      [php]$rounded_number = (ceil($number*2 0))/20;[/php]
      Mathematical trick, more than PHP.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by hsriat
        [php]$rounded_number = (ceil($number*2 0))/20;[/php]
        Mathematical trick, more than PHP.
        Nice! I enjoy seeing maths put in place as work arounds! But yeah, markus's one is probably what you're looking for!

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by markusn00b
          You didn't look hard enough, my friend!

          [php]
          echo round(1.34, 2);
          /* the second parameter is to what decimal place the number shall be rounded.
          */
          [/php]
          I don't think this works completly well as he needs it to round to a different position based on the number.

          hsriat's little exp works perfectly though if he always wants to round up.

          I've seen it before.

          -Dan

          Comment

          • alexphd
            New Member
            • Dec 2006
            • 19

            #6
            Originally posted by dlite922
            I don't think this works completly well as he needs it to round to a different position based on the number.

            hsriat's little exp works perfectly though if he always wants to round up.

            I've seen it before.

            -Dan

            Yea, I saw that in the maually and I tried using it and it didnt work, hsriat's solution does work great though. Thank you very much.

            Alex

            Comment

            Working...