Reputation:
What's the proper way to do "integer" divisions with decimal types in C# ?
I.e.
decimal a = 130, b = 60;
decimal res = a / b; //need to get 2.0, not 2.6666
Upvotes: 0
Views: 1455
Reputation: 229342
You can use Decimal.Truncate(a / b);
Decimal.Truncate() "rounds" towards zero, and is thus like Math.Floor() for positive numbers and Math.Ceiling() for negative numbers.
Upvotes: 2
Reputation: 37543
In this case I'd use the Floor
function.
decimal res = Math.Floor(a / b);
Upvotes: 6