-1

I'm using the type double to represent monetary values. As you know some number are not represented correctly, like 19,7949999999.... Is approximated to 19,80.

How can I resolve this? I tried to use Decimal instead but I still have the problem.

Thank you.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Lince81
  • 815
  • 5
  • 17
  • 30
  • 4
    You shouldn't have the same problem with decimal. What kind of calculations are you doing? (And maybe I'm misunderstanding: you say 19.794999 is approximated to 19.80 - you want the nines-recurring value? There's no way double would get rounded between 19.795 and 19.80 - they're too far apart, double has enough precision to distinguish those.) – Rup Jun 27 '11 at 09:14
  • 1
    Decimal is the best for monetary vals, show the code seems something wrong in how you dealing with it – V4Vendetta Jun 27 '11 at 09:15
  • 2
    Can you show some code using `decimal` that still shows these problems? The amount of space set aside for the mantissa in `decimal` is pretty incredible. – sarnold Jun 27 '11 at 09:15
  • See http://stackoverflow.com/questions/316727/is-a-double-really-unsuitable-for-money for more info – Jacob Jun 27 '11 at 09:19
  • Now that we have the answers, Can we have the question ! – V4Vendetta Jun 27 '11 at 09:32

2 Answers2

4

Indeed, use need to use System.Decimal.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
3

You should be using System.Decimal, which is built to represent base 10 numbers accurately.

If you already tried using Decimal, make sure you never still use double, as that could be causing the error to not go away. If you only use Decimal, you shouldn't have any issues.

George Duckett
  • 31,770
  • 9
  • 95
  • 162