The simplest formula to calculate page count?

Viewed 101153

I have an array and I want to divide them into page according to preset page size.

This is how I do:

private int CalcPagesCount()
{
    int  totalPage = imagesFound.Length / PageSize;

    // add the last page, ugly
    if (imagesFound.Length % PageSize != 0) totalPage++;
    return totalPage;
}

I feel the calculation is not the simplest (I am poor in math), can you give one simpler calculation formula?

12 Answers

The OP contains a valid answer. If I wanted to turn off paging then I could set PageSize = int.MaxValue.

Several answers here add to PageSize (imagesFound.Length + PageSize) and that could cause an overflow. Which then leads to an incorrect result.

This is the code I am going to use:

int imageCount = imagesFound.Length;

// include this if when you always want at least 1 page 
if (imageCount == 0)
{
    return 1;
}

return imageCount % PageSize != 0 
    ? imageCount / PageSize + 1 
    : imageCount / PageSize;

Always used this formula:

int totalPages = items.Count / pageSize + (items.Count % pageSize > 0 ? 1 : 0);

just semantic code, which makes explicit the partial result, and makes it obvious for any reader what is calculated. I prefer this to the compact formula :

    private int calculateNbPages(int nbResults, int pageSize)
        {
            int  nbFullyFilledPages = nbResults / pageSize;
            int nbPartiallyFilledPages = (nbResults % pageSize == 0) ? 0 : 1;
            
            return nbFullyFilledPages + nbPartiallyFilledPages;
        }
var pageCount = (int)Math.Ceiling((float)_collection.Count / (float)_itemsPerPage);

Explanation:

Divide the number of items in the collection by the items per page. Then use Math.Ceiling to round this number up to the nearest integer.

e.g. 7 Items in the 'Book' / 3 items per page = 2.33. 2.33 rounded up to the nearest int = 3.

  1. You Can Get Total Page in Sql Server:-
DECLARE @PageCount INT;
DECLARE @NoOfData INT;
SET @NoOfData = (select Count(*) AS [PageCount] from YourTableName)
SET @PageCount=((@NoOfData+@PageSize-1)/@PageSize)
SELECT @PageCount AS [PageCount]
  1. You Can get in your code-
int totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);

Following worked for me:

if(totalRecords%value === 0){
  count = (totalRecords) / value;
}
else {
  count = Math.floor((totalRecords + value - 1) / value);
}

Something I wrote myself:

private int GetPageCount(int count, int pageSize)
{
    int result = 0;

    if(count > 0)
    {
        result = count / pageSize;
        if(result > 0 && (count > (pageSize * result)))
        {
           result++;
        }
    }

    return result;
}

(And sure, don't set pageSize to int.MaxValue)

Below is working code to calculate pagination in List:

              int i = 0;
              int pagecount = 0;
              int pageSize = 50;

List Items= new List (); To do : Add items in List:

              if (Items.Count() != 0)
              {
                    int pageNumber = Total Records / 50;

                          for (int num = 0; numi < pageNumber; num++)
                          {
                                var x = Items.Skip(i * pageSize).Take(pageSize);
                                i++;
                          }

                    var y = Items.Skip(i * pageSize).Take(pageSize);
              }
Related