3

If a page can have 27 items printed on it and number of items can be any positive number then how can I find number of pages if I have number of items, I tried Modulus and division but didn't helped.

double TotalNumberOfPages = NumberOfItems/27;
int a = (int)TotalNumberOfPages; 

above code works but not logically as if double is 3.00000000000001 I want it to round up as 4 not 3, for some reason I can't use "round" method.

tereško
  • 729
  • 1
  • 8
  • 16
  • You want to round 3.00000000000001 up to 4? It's hard to get much closer to 3 than 3.00000000000001... – Yatrix Oct 30 '12 at 16:55

2 Answers2

9

Surely your TotalNumberOfPages should be an integer. In that case, try:

int PageSize = 27;
int TotalNumberOfPages = (int)Math.Ceiling((double)NumberOfItems / (double)PageSize);
Jez
  • 27,951
  • 32
  • 136
  • 233
  • The most important part of your answer is the call to `Math.Ceiling`, not the type conversion. – Zebi Oct 30 '12 at 16:57
2
(NumberOfItems + ItemsPerPage - 1) / ItemsPerPage;

In your case ItemsPerPage = 27

Igor
  • 1,835
  • 17
  • 15