简体   繁体   中英

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 ,

totalpagescount = (Totallistcount + perpagecount - 1) / perpagecount ;

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;

这是整数除法应该如何工作,你需要先将它转换为double才能获得数字,然后使用Ceiling来“舍入”:

(int)Math.Ceiling( (double)Totallistcount / perpagecount);

an other solution :

int pageCount = (records - 1) / recordsPerPage + 1;

int pageCount = (14 - 1) / 9 + 1; => pagecount = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM