0

I am getting this out put from the code which will follow:

49

49.000000000000000000000000001

I want to know how to fix it so I get back to 49.

decimal a = 49;
decimal b = 24;

decimal result = a / b;

decimal c = result * b;

Console.WriteLine(a);
Console.WriteLine(c);

the windows 10 calculator app get it right and its using more precision I guess. There has to be a way to fix this?

John
  • 1,714
  • 21
  • 41

2 Answers2

1

I think you actually want a lower precision type to do the math. If you use a double type, it works fine:

double a = 49;
double b = 24;

double result = a / b;
double c = result * b;

Console.WriteLine(a);
Console.WriteLine(c);
Rufus L
  • 36,127
  • 5
  • 30
  • 43
-2

Instead of using one number to store a quotient, use two numbers to store a quotient.

decimal a = 49;
decimal b = 24;

decimal resultNumerator = a;
decimal resultDenominator = b;

decimal c = (resultNumerator * b) / resultDenominator;

Console.WriteLine(a);
Console.WriteLine(c);
John Pick
  • 5,562
  • 31
  • 31