[英]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. 我得到23行数据,每页需要20行= 2页。
When I divide it, I get 1: 当我除以1时:
var pages = totalRows / 20;
When I use modulus, I get 3: 使用模数时,得到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. 我喜欢cdhowie的回答。 But if you don't want to use
Math.Ceiling
you can do the following: 但是,如果您不想使用
Math.Ceiling
,则可以执行以下操作:
int pages = totalRows / 20;
if (totalRows % 20 != 0) { pages += 1; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.