I have written an application which limits the records displayed, and displays next/prev links, along with page numbers for easy navigation of the result set. I am also trying to say which page the user is on, based on the total number of pages in the result set. How do I alsways round up for any result that is greater than a whole number? For example, I would like the following to "round up"

2.1 = 3
2.01 = 3
2.4 = 3
2.5 = 3
2 = 2

etc...

    not exactly. round rounds to the nearest integer - whether that be up or down, and you can specify how many decimals to round to.

    echo round(3.4);         // 3 
    echo round(3.5);         // 4 
    echo round(3.6);         // 4 
    echo round(3.6, 0);      // 4 
    echo round(1.95583, 2);  // 1.96 
    echo round(1241757, -3); // 1242000
    

    you can use the ceil() command (short for ceiling) to round a number up to the nearest integer, or floor() to round down every time.

    ads

      Thanks - looks like I was looking for the 'ceil' function.

        For anyone else doing research on the 'round' function, this tip should be taken into account, because round doesn't always round up (taken from user contributed notes on the 'round' function:

        Here is a more detailed description, taken from
        http://www.progressive-comp.com/Lists/?l=php3-general&m=93230944323512&w=2

        I tried
        round(11.5) and I get 12
        round(182.5) and I get 182...

        You are right.
        It could be a bug in the round() function.

        This is normal and reflects how computers store floating point numbers.
        11.5 is not actually 11.5. It is 11.49999999999999999 or
        11.50000000000000000001. Floating point numbers are approximated using
        some level of precision. Think of 1/3. How would you write that?
        0.3333333333... when do you stop? How many 3's do you need to accurately
        write 1/3? To be completely accurate you need an infinite amount of 3's
        and that just isn't feasible, so at some point you just have to say that
        you have enough precision.

        This means that round(11.5) is not defined. If you want to always round
        up on .5, you should add some small (less than your desired precision)
        value to the number you feed to round. And if you want to always round
        down on .5 you should substract it.

        eg.

        $fuzz = 0.00000001;
        echo round(11.5 + $fuzz);

        -Rasmus

          Originally posted by bruceg
          > I tried

          round(11.5) and I get 12
          round(182.5) and I get 182...

          You are right.
          It could be a bug in the round() function.



          This is normal and reflects how computers store floating point numbers.
          11.5 is not actually 11.5.

          There are two errors in the statements above. There is no bug. First, a round function which rounds odd numbers up and even numbers down is called bankers rounding. It is done that way because of the distribution of integers above and below the "breakpoint" number 0.5. 0,1,2,3,4 = 5 integers. 5,6,7,8,9 = 5 integers. Values of exactly 0.5 always go the nearest even number, so there is an even distribution of "up" and "down" rounding.

          Second, 11.5 can be represented as EXACTLY as 11.5 in any standard floating point field in use on computers today. Here is a small tutorial on why some floating point numbers can be represented exactly, while others cannot.

          It is not possible to exactly represent many decimal fractions in a binary float field. For example, 0.1 cannot be represented exactly in a 32 bit float with a 23 bit mantissa. It is not the number of decimal digits, but rather whether the number can be represented in terms of 1 / (2n) where n is the bit position in the mantissa. For example

          bit 1 - 1 / (21) = 0.5
          bit 2 - 1 / (22) = 0.25
          bit 3 - 1 / (23) = 0.125
          bit 4 - 1 / (24) = 0.0625
          and so on until you get to
          bit 23 - 1 / (223) = 0.00000011920928955078125

          No matter how you combine the bits you cannot get to 0.1, although you can get close.

            Write a Reply...