-4

I don't know how to remove the tail of zero on a "double" conversion from a generic currency string in C#.

This is my code

double reddito = Math.Round(Convert.ToDouble("12500,245"), 3);

the expected result

reddito = 12500.245

the real result

reddito = 12500.245000000001

what is the matter?

1 Answers1

1

For currencies it is best to use a decimal type rather than a double. Doubles and floats are essentially approximations of the real number and can quickly get you into trouble with financial calculations. Recommended practice is not to test floats and doubles for equality but allow for a small tolerance around the value for this particular reason.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61