Sunday, 3 January 2010

Pagination - rounding up my number of pages

I had to manually code a bit of pagination in C# today. To work out how many pages there are in total I started to look into testing for an integer when dividing the results by the results-per-page. Instead I used this simple bit of maths:
numberOfPages = (totalResults + resultsPerPage - 1)/resultsPerPage;
And, if you're interested, here's the complicated way of doing it:
pageCount = int.Parse(Math.Ceiling(Decimal.Divide(TotalCount, PageSize)));
if (pageCount == 0) pageCount = 1;

1 comment:

  1. dear friend thanks your great help... i've tried many option using math .round/floor/ciel but nothing gave me a proper result... and none other site where having this solution... thanks again


Comments are moderated, so you'll have to wait a little bit before they appear!