M Kenyon II
M Kenyon II

Reputation: 4264

Why does C# Math.Ceiling round down?

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)'

Upvotes: 13

Views: 7170

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

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))

Upvotes: 33

IS4
IS4

Reputation: 13187

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));

Upvotes: 13

Related Questions