user964970
user964970

Reputation:

Integer division with decimals

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

Answers (3)

nos
nos

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

arian
arian

Reputation: 116

decimal a = 130, b = 60;

decimal res = Math.Floor(a/b);

Upvotes: 4

Joel Etherton
Joel Etherton

Reputation: 37543

In this case I'd use the Floor function.

decimal res = Math.Floor(a / b);

Upvotes: 6

Related Questions