简体   繁体   中英

Count how many pages are needed

How do I count how many pages my data needs to fit?

I got 23 rows of data and every page takes 20 rows = 2pages.

When I divide it, I get 1:

var pages = totalRows / 20;

When I use modulus, I get 3:

var pages = totalRows % 20;

So please explain to me what calculation I should use to resolve this issue.

您应该先划分然后四舍五入,这将占最终页面少于20行的原因。

var pages = (int)Math.Ceiling(totalRows / 20.0);

Try

var pages = (totalRows+19) / 20;

in general:

var pages = (totalRows+rowsPerPage-1) / rowsPerPage;

that is equivalent to doing a floating point division and rounding up (see other answer)

I like cdhowie's answer. But if you don't want to use Math.Ceiling you can do the following:

int pages = totalRows / 20;
if (totalRows % 20 != 0) { pages += 1; }

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