NetRunner
NetRunner

Reputation: 149

in C# (int) decimal and Convert.Int32(decimal) give me two different results

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

Upvotes: 4

Views: 494

Answers (3)

rahul
rahul

Reputation: 187040

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

Upvotes: 3

mtmk
mtmk

Reputation: 6316

I wonder if its precedence issue? Try this:

(int)(amount*1000);

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062780

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

Upvotes: 15

Related Questions