next bigger interger

August 12, 2008 by pierre

I’m working on PDF templates using FPDF, the templates are based on a grid and I had to find how much of the grid’s units different images are using, e.g. if an image width is 1.3 units I need to make 2 units space for it in the the grid.

In other words I needed to “round” a floating point number, but always to a bigger number so the round php function would not be usefull :

echo round(1.2); // 1
echo round(1.6); // 2

Then I thought that what I wanted was the “next bigger integer”, and it felt in place : change the float to an integer and add one.

echo ((int) 1.2) + 1; // 2
echo ((int) 1.6) + 1; // 2

This is super simple but it seem to me a good exemple of how formulating what you want to do in english can help when programming.


4 Comments »

  1. une rentrée sur les chapeaux de roues!
    welcome back!!!

    Comment by jerome — August 13, 2008 @ 11:37 am

  2. neat.

    Comment by kosmar — August 28, 2008 @ 4:58 am

  3. ceil($var) might be more computationally efficient… or is that just spoiling the fun?

    Comment by sts — October 3, 2008 @ 12:08 pm

  4. hello simon - you’re right: it seems like…

    however i quite like the point pierre is making here saying: formulating in in plain english what one wants to achieve helps to conceptualise the idea and make it into code - but again, maybe, one should check the manual first …

    ;)

    Comment by jerome — October 5, 2008 @ 7:52 am

RSS feed for comments on this post. TrackBack URL

Leave a comment