Alex Rivera | Logout

Divide not returning the decimal value I expect

Asked 2010-04-08T01:36:46.367
24

Possible Duplicate:
What's wrong with this division?

If you divide 2 / 3, it should return 0.66666666666666667. Instead, I get 0.0 in double value type and 0 in decimal.

My purpose is to divide even (e.g. 2 / 3) and round to 1 always to the nearest.

Any help?

Edit
Report

1 Answer

56

You're doing integer division, from the sounds of it. Try this:

decimal result = 2.0 / 3.0;

Or even force it to decimals for all of the operations:

decimal result = 2.0m / 3.0m;

This should give you a result more like you expect.

answered 2010-04-08T01:38:16.997

Your Answer