Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always get a whole number if we divide two integers in c#

I have three integer type variable

  1. Totallistcount
  2. totalpagescount
  3. perpagecount

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 ,

like image 332
Smartboy Avatar asked Nov 14 '12 08:11

Smartboy


2 Answers

totalpagescount = (Totallistcount + perpagecount - 1) / perpagecount ;
like image 176
dtb Avatar answered Sep 19 '22 18:09

dtb


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);
like image 20
Vyktor Avatar answered Sep 19 '22 18:09

Vyktor