Reputation: 1006
I have three integer type variable
Suppose at initial level i have this
Totallistcount = 14;
perpagecount = 9;
Now I have a formula to found total number of pages possible
totalpagescount = Totallistcount / perpagecount ;
but in this situtation I got 1
in totalpagescount
but I need 2
in totalpagescount
, because 9 items on the first page and rest of item will be displayed on last page , How can I do this
Thanks ,
Upvotes: 5
Views: 26010
Reputation: 451
an other solution :
int pageCount = (records - 1) / recordsPerPage + 1;
int pageCount = (14 - 1) / 9 + 1; => pagecount = 2
Upvotes: 1
Reputation: 20997
This is how integer division should work, you need to convert it to double
first to be able to get the number and then use Ceiling
to "round it up":
(int)Math.Ceiling( (double)Totallistcount / perpagecount);
Upvotes: 5
Reputation: 50104
If you want to round up, you need to perform the division as a floating point number, then call Math.Ceiling
to get the next-highest whole number.
double quotient = Totallistcount / (double)perpagecount;
double ceiling = Math.Ceiling(quotient);
int totalpagescount = (int)ceiling;
Upvotes: 3
Reputation: 217233
totalpagescount = (Totallistcount + perpagecount - 1) / perpagecount ;
Upvotes: 17