snippetjavaCritical
How to round up the result of integer division?
Viewed 0 times
howintegertheroundresultdivision
Problem
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?
If I have x items which I want to display in chunks of y per page, how many pages will be needed?
Solution
Found an elegant solution:
Source: Number Conversion, Roland Backhouse, 2001
int pageCount = (records + recordsPerPage - 1) / recordsPerPage;Source: Number Conversion, Roland Backhouse, 2001
Code Snippets
int pageCount = (records + recordsPerPage - 1) / recordsPerPage;Context
Stack Overflow Q#17944, score: 591
Revisions (0)
No revisions yet.