Which data type should I use for a money value?

R

Ronald S. Cook

For money values, C# seems to have just DOUBLE as a type.

SQL Server 2005 has data types DECIMAL, FLOAT, MONEY, and NUMERIC that seem
to all be able to hold a money time.

So, given I'll be storing money as a double in code, what datatype should I
have in the database for when I pass the value. I.e. which of all the SQL
types best matches up?

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

You would want to use the Decimal type, as it will give you the
precision you need. This is what you would use in .NET code.

For the database, you can use the money class, assuming that you are not
going to store more than 1/10000th of your currency unit. While I don't
know of any currencies that have 0 decimal places, I imagine that they used
four decimal places in case you are storing intermediate values which will
have operations performed on them before they are returned to the user.

If you have a need for greater precision in the database, then you can
use the decimal type, and indicate the precision and scale yourself.

Hope this helps.
 
B

Bruce Wood

For money values, C# seems to have just DOUBLE as a type.

SQL Server 2005 has data types DECIMAL, FLOAT, MONEY, and NUMERIC that seem
to all be able to hold a money time.

So, given I'll be storing money as a double in code, what datatype should I
have in the database for when I pass the value. I.e. which of all the SQL
types best matches up?

DON'T store monetary quantities as doubles in code. Use decimal.

If you use doubles then you leave yourself open to rounding errors.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Ronald said:
For money values, C# seems to have just DOUBLE as a type.

Wrong.

It has decimal that are intended for it.
SQL Server 2005 has data types DECIMAL, FLOAT, MONEY, and NUMERIC that seem
to all be able to hold a money time.

So, given I'll be storing money as a double in code, what datatype should I
have in the database for when I pass the value. I.e. which of all the SQL
types best matches up?

decimal in C# and either DECIMAL/NUMERIC (it is the same) or MONEY in
the database.

Arne
 
T

tylerxprice

In addition to using decimal to store the value amount, Money may
sufficiently complex that it should be its own class: fields such as
amount, currency (or cultural information), and methods for displaying
different formats and comparison could be useful.
 
M

Marc Gravell

In addition to using decimal to store the value amount, Money may
sufficiently complex that it should be its own class: fields such as
amount, currency (or cultural information), and methods for displaying
different formats and comparison could be useful.

Actually, this could be one of the (rare) occasions when the correct
approach is to create a struct (immutable of course) rather than a
class. Over a good-few years of .Net programming I can only remeber
writing a handful (or less) of structs; a currency/amount pair was one
of them.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top