How to round up the result of integer division?

Ian Nelson picture Ian Nelson · Aug 20, 2008 · Viewed 207.9k times · Source

I'm thinking in particular of how to display pagination controls, when using a language such as C# or Java.

If I have x items which I want to display in chunks of y per page, how many pages will be needed?

Answer

Ian Nelson picture Ian Nelson · Aug 20, 2008

Found an elegant solution:

int pageCount = (records + recordsPerPage - 1) / recordsPerPage;

Source: Number Conversion, Roland Backhouse, 2001