views:

257

answers:

6

What is the best currency format to use in my database in conjunction with my ASP.NET web app. I am not sure if float or decimal is better. Please note, I will not need culture specific settings.

+1  A: 

After reading more about credit card processing, I'd be willing to try integers (use cents instead of dollars). If you're using dollar amounts, I'd use decimal.

The big issue with currency and datatypes has been rounding errors with floats, so I'd stay away from those.

Jon Smock
Using INTEGERS to represent anything but a whole amounts leads to interesting problems down the road. This is true for everything from bathrooms in a house (it's 2.5 baths, not 5 half baths units) to dollars (and fractional dollars, i.e. cents). Mostly, different consumers of the data will not understand/realize that the data refer to fractional quantities .
Alex Papadimoulis
A: 
int no=10000;
Console.WriteLine(no.ToString("N2"));
adatapost
+1  A: 

I would go with decimal. You want to keep as much precision as you can. People that coded financial apps know what I am talking about when the credit is almost equal with debit but not just right. This is the reason why the gain and loss banking account are.

Claudiu
+1  A: 

You do not want to go with FLOAT for representing amounts like money. Bad things happen with rounding, especially the values are aggregated, added, manipulated, etc.

If you need fractional dollars (i.e. cents), then DECIMAL is the appropriate choice. Otherwise, you may as well stick with INTEGERS as not to create problems with pennies

Alex Papadimoulis
+5  A: 

Do not use float.

decimal, at a specification of decimal(17, 4) is probably acceptable. That means '17' places to the left, and to 4 decimal places.

Noon Silk
As an added bonus, having 17 places on the left will accommodate ultra-hyper-mega inflation, i.e. Zimbabwe-style on steroids. On second thought, better make it 30 on the left, just in case we see a *super*-ultra-hyper-mega-insano-wowie inflation. You never know when things will cost $(more atoms than in the universe).00!
Alex Papadimoulis
If the OP is doing forex, he'll need more than 4 decimal places. Currency exchange quotes come with higher precision.
Esteban Araya
+6  A: 

If you're using SQL Server, you have the 'money' type, which translates to a decimal type in .NET.

Decimal is a 128bit base 10 floating point number, rather than a binary floating point (which float/long are), which makes it so that in most cases it doesn't lose precision, unlike binary floating point numbers, which have accuracy loss as part of their design.

Here's a nice article explaining decimals and floating point logic in general.

Ilia Jerebtsov
+1: I thought Money would be the obvious type...
SnOrfus
Binary floating point numbers actually have much *greater* accuracy (per byte used) than decimal floating point numbers. (.NET decimals consume *twice* as many bytes as IEEE-754 double precision floats, but are less than twice as precise). So for anything involving non-financial number-crunching, normal floats are actually much better. But yes, for money purposes you want a decimal type.
Daniel Pryden
Daniel: Yes, thank you, I guess I could have made it more clear with what I meant by "accuracy loss".
Ilia Jerebtsov