计算页数的最简单公式是什么?

我有一个数组,我想分成网页,根据预设的网页大小。

我就是这么做的:

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


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

我觉得计算不是最简单的(我数学很差) ,你能给我一个简单的计算公式吗?

104422 次浏览

Force it to round up:

totalPage = (imagesFound.Length + PageSize - 1) / PageSize;

Or use floating point math:

totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);

Actually, you are close to the best you can do. About the only thing that I can think of that might be "better" is something like this:

totalPage = (imagesFound.Length + PageSize - 1) / PageSize;

And the only reason that this is any better is that you avoid the if statement.

To avoid having errors with page numbering the best way I can think of calculating noOfPages is by doing the following line

totalPage = Math.Ceiling(imagesFound.Length / PageSize);

This should not give you page 2 when PageSize == imagesFound.Length

NOTE: you will always get at least 1 page, even for 0 count, if the page size is > 1, which is what I needed but may not be what you need. A page size of 1(silly but technically valid) and a count of 0 would be zero pages. Depending on your needs you may want to check for a zero value for count & page size of 1

int pages = ((count - 1) / PAGESIZE) + 1;

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;
  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);


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);
}

Following worked for me:

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

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;
}

Always used this formula:

int totalPages = items.Count / pageSize + (items.Count % pageSize > 0 ? 1 : 0);
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.

I used the function below. It does not use floating point calculation, so it is faster than some other solutions I have seen.

int GetPageCount()
{
//when pageSize is 0, return all records on one page
return (pageSize != 0) ? (recordCount / pageSize + Math.Sign(recordCount % pageSize)) : 1;
}