C #: pagination, math.

I am creating some pagination and I have a problem.

If I have number 12 and I want to divide it by 5 (5 is the number of results that I want on the page), how would I think it right? This does not work:

int total = 12;
int pages = Math.Ceiling(12 / 5);
//pages = 2.4... but I need it to be 3
+5
source share
2 answers

Although your code should work, it’s Math.Roundincorrect, you can try the following:

int pages = (total + pageSize - 1)/pageSize;

It should be the same as Math.Ceiling, except that you always deal with int, and not doubleat any time, when it returns Math.Ceiling.

EDIT: To make your code work, you can try:

int pages = (int)Math.Ceiling((double)12/(double)5);

.

+13

:

int numPages = Math.Ceiling((decimal)12 / (decimal)5);

int numPages = (12 + 4) / 5;  //(total + (perPage - 1)) / perPage
+6

All Articles