C# 计算页数的最简单公式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1611235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The simplest formula to calculate page count?
提问by Benny
I have an array and I want to divide them into page according to preset page size.
我有一个数组,我想根据预设的页面大小将它们分成页面。
This is how I do:
这就是我的做法:
private int CalcPagesCount()
{
int totalPage = imagesFound.Length / PageSize;
// add the last page, ugly
if (imagesFound.Length % PageSize != 0) totalPage++;
return totalPage;
}
I feel the calculation is not the simplest (I am poor in math), can you give one simpler calculation formula?
感觉计算不是最简单的(我数学不好),能不能给出一个更简单的计算公式?
采纳答案by John Kugelman
Force it to round up:
强制它四舍五入:
totalPage = (imagesFound.Length + PageSize - 1) / PageSize;
Or use floating point math:
或使用浮点数学:
totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);
回答by Tom
Actually, you are close to the best you can do. About the only thing that I can think of that might be "better" is something like this:
事实上,你已经接近你能做到的最好了。我唯一能想到的可能是“更好”的是这样的:
totalPage = (imagesFound.Length + PageSize - 1) / PageSize;
And the only reason that this is any better is that you avoid the if statement.
这样做更好的唯一原因是您避免使用 if 语句。
回答by Clayton C
To avoid having errors with page numbering the best way I can think of calculating noOfPages is by doing the following line
为了避免出现页码错误,我能想到的计算 noOfPages 的最佳方法是执行以下行
totalPage = Math.Ceiling(imagesFound.Length / PageSize);
This should not give you page 2 when PageSize == imagesFound.Length
当 PageSize == imagesFound.Length 时,这不应该给你第 2 页
回答by Booji Boy
NOTE: you will always get at least 1 page, even for 0 count, if the page size is > 1, which is what I needed but may not be what you need. A page size of 1(silly but technically valid) and a count of 0 would be zero pages. Depending on your needs you may want to check for a zero value for count & page size of 1
注意:如果页面大小 > 1,您将始终获得至少 1 页,即使是 0 计数,这是我需要的,但可能不是您需要的。页面大小为 1(愚蠢但技术上有效)和计数为 0 将是零页。根据您的需要,您可能需要检查计数和页面大小的零值是否为 1
int pages = ((count - 1) / PAGESIZE) + 1;
回答by Jeremy
The OP contains a valid answer. If I wanted to turn offpaging then I could set PageSize = int.MaxValue
.
OP 包含有效答案。如果我想关闭分页,那么我可以设置PageSize = int.MaxValue
.
Several answers here add to PageSize
(imagesFound.Length + PageSize
) and that could cause an overflow. Which then leads to an incorrect result.
这里的几个答案添加到PageSize
( imagesFound.Length + PageSize
) 中,这可能会导致溢出。然后导致错误的结果。
This is the code I am going to use:
这是我将要使用的代码:
int imageCount = imagesFound.Length;
// include this if when you always want at least 1 page
if (imageCount == 0)
{
return 1;
}
return imageCount % PageSize != 0
? imageCount / PageSize + 1
: imageCount / PageSize;
回答by vikas Chaturvedi
- You Can Get Total Page in Sql Server:-
- 您可以在 Sql Server 中获取总页面:-
DECLARE @PageCount INT;
DECLARE @NoOfData INT;
SET @NoOfData = (select Count(*) AS [PageCount] from YourTableName)
SET @PageCount=((@NoOfData+@PageSize-1)/@PageSize)
SELECT @PageCount AS [PageCount]
- You Can get in your code-
- 你可以输入你的代码-
int totalPage = (int) Math.Ceiling((double) imagesFound.Length / PageSize);
回答by st_stefanov
Something I wrote myself:
我自己写的东西:
private int GetPageCount(int count, int pageSize)
{
int result = 0;
if(count > 0)
{
result = count / pageSize;
if(result > 0 && (count > (pageSize * result)))
{
result++;
}
}
return result;
}
(And sure, don't set pageSize
to int.MaxValue
)
(当然,不要设置pageSize
为int.MaxValue
)
回答by Jugendra Singh
Below is working code to calculate pagination in List:
以下是计算 List 中分页的工作代码:
int i = 0;
int pagecount = 0;
int pageSize = 50;
List Items= new List (); To do : Add items in List:
列表项=新列表(); 要做:在列表中添加项目:
if (Items.Count() != 0)
{
int pageNumber = Total Records / 50;
for (int num = 0; numi < pageNumber; num++)
{
var x = Items.Skip(i * pageSize).Take(pageSize);
i++;
}
var y = Items.Skip(i * pageSize).Take(pageSize);
}
回答by QauseenMZ
Following worked for me:
以下为我工作:
if(totalRecords%value === 0){
count = (totalRecords) / value;
}
else {
count = Math.floor((totalRecords + value - 1) / value);
}