0

I am working on developing custom pager control to my grid view.

I am using the following syntax to get no of pages:

dblpagecount = (doble)(totrecords / grdPages.PageSize);
pagecount = (int)Math.Ceiling(dblPagecount);

Using above syntax , if the no of records is 41 and page size is 5 then I am getting pagecount as 8 which should be 9.

If I use the below syntax,

double dblPagecount = (double)((decimal)totrecords /grdPages.PageSize);
pagecount = (int)Math.Ceiling(dblPagecount);

I am getting exact page count i.e 9

I got the desired result , but unable to understand why the above synatax is not giving desired results.

When I debugged in first case, I observed that dblpagecount is getting result as 8.0 insted of 8.2

Can any one please clarify , how the above statements works ?

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96

2 Answers2

1

I'm assuming that both totrecords and grdPages.PageSize are of type int. Therefore, regardless of the type of dblpagecount, you'll end up with the division being performed in integer arithmetic, which truncates towards 0. However, I wouldn't actually use your second piece of code (and certainly wouldn't use both double and decimal - mixing those is almost always a bad idea). Instead, I'd just use:

// Names changes slightly for readability
int pageCount = (totalRecords + grdPages.PageSize - 1) / grdPages.PageSize;

This will effectively "round up" without ever requiring any floating point operations.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Jon Skeet performing integer division results in no floating-point operations whatsoever? I guess I assumed that floating-point operations occur regardless (it is division), but the result is effectively truncated. – Christopher Rucinski Sep 26 '13 at 08:55
  • @ChristopherRucinski: It would be an integer machine code operation. I would really hope that wouldn't need to go via floating point code... – Jon Skeet Sep 26 '13 at 08:56
0

Simple answer

int / int = int

If you want to get a decimal-type number (math-speak here, aka any number with a decimal in it from 3.0 to 3.14159) you have to do something like...

double / int = double

int / double = double

So just cast one value, and you are good, and that is why the following works...

double dblPagecount = (double)((decimal)totrecords /grdPages.PageSize);

Please note that the outer cast is NOT needed, and the inner cast should be of type double! Please read my comment below to get the general idea of what you are doing here.

Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
  • by the way, the outer `double` cast I think is unnecessary. `DecimalTotRecords / IntGrdPages_PageSize` results in a `double` expression. You then cast that double to a double, and then assign to `dblPagecount` – Christopher Rucinski Sep 26 '13 at 08:51
  • @ChristopherRucinski: No it doesn't - dividing a `decimal` by an `int` gives a `decimal`, not a `double`. This is really *not* a good approach. It's overly complicated, and mixes different floating point types completely pointlessly. – Jon Skeet Sep 26 '13 at 09:07
  • @JonSkeet yes, sorry, I was a little more sleepy than I thought. The *general* idea is right, and I updated my answer. The OP understood generally what I was saying it seems, and I wanted to provide an insight into how to evaluate the expressions. – Christopher Rucinski Sep 27 '13 at 00:17
  • I still disagree with "and you are good" though - I would still prefer to see the "no floating point required" approach, personally. – Jon Skeet Sep 27 '13 at 05:36