Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The simplest formula to calculate page count?

Tags:

c#

pagination

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?

like image 361
Benny Avatar asked Oct 23 '09 03:10

Benny


People also ask

How do you calculate number of pages?

To calculate the page count for a 5.5″ × 8.5″ book: 10 pt type – divide your word count by 475. 11 pt type – divide your word count by 425. 12 pt type – divide your word count by 350.

How does PHP calculate total pages of pagination?

php $pages = get_posts('category_name=news'); ?> divide that number by 1 and give how many times 1 goes into it. thus giving as many page numbers as needed.


1 Answers

Force it to round up:

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

Or use floating point math:

totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize); 
like image 108
John Kugelman Avatar answered Sep 27 '22 23:09

John Kugelman