C#:分页、Math.Ceiling
我正在创建一些分页,但遇到了问题。
如果我有一个数字 12,并且我想将其除以 5(5 是我想要在一页上显示的结果数),我该如何正确地将其四舍五入?这不起作用:
int total = 12;
int pages = Math.Ceiling(12 / 5);
//pages = 2.4... but I need it to be 3
I'm creating some pagination and I'm getting an issue.
If I have a number 12 and I want to divide that by 5 (5 is the number of results I want on a page), how would I round it up properly? This doesn't work:
int total = 12;
int pages = Math.Ceiling(12 / 5);
//pages = 2.4... but I need it to be 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您的代码应该可以工作,但
Math.Round
是错误的,您可以尝试这样做:这应该与
Math.Ceiling
相同,只是您总是处理 <当Math.Ceiling
返回时,code>int 而不是double
。编辑:要让您的代码正常工作,您可以尝试:
但您应该使用第一个示例。
Even though your code should work,
Math.Round
is wrong though, you could try this:That should be the same as
Math.Ceiling
except that you are always dealing withint
and notdouble
at any point asMath.Ceiling
returns.EDIT: To get your code to work you could try:
But you should use the first example.
你可以这样做:
或者
you could do:
or