Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# Math.Ceiling round down?

Tags:

c#

math

I'm having a rough day, but something is not adding up correctly.

In my C# code, I have this:

Math.Ceiling((decimal)(this.TotalRecordCount / this.PageSize))

Where (int)TotalRecordCount = 12 and (int)PageSize = 5. The result I am getting is 2.
(Both values are int values.)

By my calculations, 12 / 5 = 2.4. I thought Math.Ceiling would always round up, and in this case, give me 3?

PS, if I do this:

Math.Ceiling(this.TotalRecordCount / this.PageSize)

I get the message:

Math.Ceiling(this.TotalRecordCount / this.PageSize)
The call is ambiguous between the following methods or properties:
'System.Math.Ceiling(decimal)' and 'System.Math.Ceiling(double)'

like image 815
M Kenyon II Avatar asked Apr 05 '16 15:04

M Kenyon II


2 Answers

You see "rounding down" because the truncation happens before reaching Math.Ceiling.

When you do this

(this.TotalRecordCount / this.PageSize)

It is an integer division, and its result is a truncated int; it is too late to cast it to decimal.

To fix this problem, cast before the division:

Math.Ceiling(((decimal)this.TotalRecordCount / this.PageSize))
like image 163
Sergey Kalinichenko Avatar answered Nov 09 '22 21:11

Sergey Kalinichenko


Because TotalRecordCount and PageSize is int, and int division rounds down. You have to convert at least one of the operands to decimal to use decimal division:

Math.Ceiling((decimal)this.TotalRecordCount / this.PageSize));
like image 29
IS4 Avatar answered Nov 09 '22 21:11

IS4