The simplest formula to calculate page count?
C#PaginationC# Problem Overview
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?
C# Solutions
Solution 1 - C#
Force it to round up:
totalPage = (imagesFound.Length + PageSize - 1) / PageSize;
Or use floating point math:
totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);
Solution 2 - C#
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;
Solution 3 - C#
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.
Solution 4 - C#
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;
Solution 5 - C#
Always used this formula:
int totalPages = items.Count / pageSize + (items.Count % pageSize > 0 ? 1 : 0);
Solution 6 - C#
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;
}
Solution 7 - C#
- 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]
- You Can get in your code-
int totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);
Solution 8 - C#
Following worked for me:
if(totalRecords%value === 0){
count = (totalRecords) / value;
}
else {
count = Math.floor((totalRecords + value - 1) / value);
}
Solution 9 - C#
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
Solution 10 - C#
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
)
Solution 11 - C#
Below is working code to calculate pagination in List:
int i = 0;
int pagecount = 0;
int pageSize = 50;
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);
}
Solution 12 - C#
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.