2

I can't understand what is wrong? I have a little variable.

decimal Price = 22348 / 100;

The answer I get is: 223, but I should get 223.48. What is the problem?

Omar Lers
  • 27
  • 7
  • 1
    Possible duplicate of [How can I divide two integers to get a double?](http://stackoverflow.com/questions/661028/how-can-i-divide-two-integers-to-get-a-double) – MakePeaceGreatAgain Mar 24 '17 at 10:35

2 Answers2

11

What you are doing is this:

decimal = (decimal)(int / int);

So you are calculating integers (yielding the result you get) and then convert the outcome to a decimal.

Instead, you could cast either of the operands to a decimal:

decimal Price = 22348 / 100M;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Declare those numbers as decimal variable then put the divisions result in the other decimal variable.

    decimal num1 = 22348;
    decimal num2 = 100;
    decimal Price = (num1 / num2);
    Console.WriteLine(Price);

This will give the result 223.48

Programmer
  • 11
  • 2