tags:

views:

166

answers:

3

Hi,

I understand that there are rounding errors but can anyone explain why I get such different results using these different methods:

decimal amount = 9.990M;
var cost = Convert.ToInt32(amount*1000);
var cost1 = (int) amount*1000;

I get:

cost = 9990
cost1 = 9000
+15  A: 

Try (int)(amount*1000). In the Convert, the brackets enforce the precedence, but cast (int) takes precedence over the multiply - so you current have: ((int)amount)*1000, which rounds (during the cast) to 9.

In particular, see "7.2.1 Operator precedence and associativity" in the MS spec, which defines cast ahead of multiplication:

  • 7.5: Primary: x.y f(x) a[x] x++ x-- new typeof default checked unchecked delegate
  • 7.6: Unary: + - ! ~ ++x --x (T)x
  • 7.7: Multiplicative: * / %
  • etc
Marc Gravell
+2  A: 

I wonder if its precedence issue? Try this:

(int)(amount*1000);
Maxwell Troy Milton King
+3  A: 

The second one should be

var cost1 = (int)(amount * 1000);

You have to multiply with 1000 and then convert the result. In your example you are converting first and then multiplying.

See Operator precedence and associativity

rahul