Michael Knopf

turning concepts into working products...

Double vs. Decimal - Why you need to know the difference
Michael Knopf
Monday, August 04, 2008 | Share on Facebook RSS Feeds

Both Double and Float are calculated values, meaning they can result in “off by one” errors. What's an "off by one" error and why should i care? 

Both the Double and Float values in .NET will have their decimal points truncated which results in precision errors. This really comes in to play when dealing with money of any kind, especially if you use the Math.Round() method

All the built in .NET Math functions available in C# take double values. If you were planning on writing an application that worked with money of any kind (Dollars, Yen, Euro, etc...) your money application will use rounding, its a common pitfall to think that its safe to use these built in libraries, think again. 

If you round two values using these you will discard the remainder and your data will be compromised from that point forward. Anyone who has ever seen the movie Hackers will immediately recognize that this minute error can and will become an enormous error in a very short period of time. 

If you were going to write a money application the very first you will want to do is rewrite the Math class and the Rounding methods.