DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

Decimal data type for financial applications
My current project uses a double data type for its values and i have raised a suggestion to convert it to decimal data type instead.  I remember that it is recommended to use decimal over double for financial applications because decimal data types have greater significant digits compared to floating points.  Floating point types is composed of float and double data types for c#.

decimal: 28-29 significant digits
float: 7 digits
double: 15-16 digits

[EDIT]
Even if your application reached million in data representation, you're not safe if you use float since a million contains 7 whole number digits plus the possible 2 digits for the decimal, a total of 9 significant digits.  You'll end up with rounded off data which is problematic for financial apps.  Note that you should also use the correct data type in your data store (money for SQL server).

Posted 09-07-2006 1:39 PM by jokiz
Filed under: , , ,

Comments

jop wrote re: Decimal data type for financial applications
on 09-07-2006 7:35 AM
How about making it an a type instead; Money class perhaps? Calculating it or displaying it would then be the responsibility of the type. You can make it bahave as a double or a decimal by using operator overloading the necessary operators and when you want to print it out, call ToString instead. If you are really paranoid about space, you can make a boatload of classes implementing the IMoney interface and let the Money class use the most appropriate representation as required. If the money is a whole number, it will use the IntMoney. If certain calculations resulted in the money having cents, then use decimal. If you are working on a freak calculation running into millions of digits of money (wow), then revert to a string representation or a multi-byte implementation instead. This all happens inside the Money class. Users of money can use the familiar interface using mathematical operators and such. Of course, it sounds complicated and too much work. However, in a financial application where Money is the center of everything, don't you think it is all worth it?
cruizer wrote re: Decimal data type for financial applications
on 09-07-2006 7:26 PM
YAGNI :) the only reason I'll think of doing such a Money type is to accommodate multiple currencies
jop wrote re: Decimal data type for financial applications
on 09-10-2006 8:23 PM
Of course I am not suggesting that he implement a full blown multicurrency money class right away :-). YAGNI would apply right away because I am implementing something that I "think" I would need in the future. However, what I am trying to say is use the Money class to delay the design decision until such a time that you have more information about the problem. Instead of deciding *now* whether to use double or decimal, let the application be built around a Money class and then let Money internally use double. If you found out in the future that you will need to use decimal instead, then the changes that you would need to make is isolated in the Money class. A single currency money class is simple to make and only needs to support limited functionality (addition or subtraction? I've never heard of someone finding the sin or cosine of Monies, yet). :-).
velocity wrote re: Decimal data type for financial applications
on 09-17-2006 11:12 PM
galing talaga ni uncle jop...hawig kay uncle bob. So what you're saying is "delaying design decision until the last responsible moment is another way of saying YAGNI"? While there seems to be some overhead in coming up with a new type, I would agree with Jop that this will save you grief later on...unless this part of your code is a one-off or tangential to the application.
jop wrote re: Decimal data type for financial applications
on 09-18-2006 5:36 AM
Hey, someone here agrees with me. That's odd? :D
Amoxicillin acne. wrote Amoxicillin false positive for cocaine.
on 04-13-2008 4:01 AM

Amoxicillin. Safety of amoxicillin and greyhounds. Amoxicillin alternative. Dosing of amoxicillin for sinus infection. Amoxicillin and strep b. Bleeding and amoxicillin.

http://sms4smile.com/?page=c8/79838 wrote mature webcams housewife galleries
on 05-27-2008 3:24 PM

eAPQHt0 | chubby mature webcams granny


Copyright DevPinoy 2005-2008