Connect with Expertise | Find Experts, Get Answers, Share Insights

Which data type should I use for a money value?

Ronald S. Cook
 
Posts: n/a
#1: Apr 9 '07
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



Nicholas Paldino [.NET/C# MVP]
 
Posts: n/a
#2: Apr 9 '07

re: Which data type should I use for a money value?


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.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Ronald S. Cook" <rcook@westinis.comwrote in message
news:enyYb4ueHHA.3884@TK2MSFTNGP04.phx.gbl...
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
>
>

Bruce Wood
 
Posts: n/a
#3: Apr 9 '07

re: Which data type should I use for a money value?


On Apr 9, 2:30 pm, "Ronald S. Cook" <r...@westinis.comwrote:
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?=
 
Posts: n/a
#4: Apr 9 '07

re: Which data type should I use for a money value?


Ronald S. Cook wrote:
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
tylerxprice@gmail.com
 
Posts: n/a
#5: Apr 10 '07

re: Which data type should I use for a money value?


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.

Marc Gravell
 
Posts: n/a
#6: Apr 10 '07

re: Which data type should I use for a money value?


On 10 Apr, 01:45, tylerxpr...@gmail.com wrote:
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

Closed Thread

Tags
c# money, money c#