363,925 Members | 2607 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Which data type should I use for a money value?

Ronald S. Cook
P: n/a
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


Apr 9 '07 #1
Share this Question
Share on Google+
5 Replies


Nicholas Paldino [.NET/C# MVP]
P: n/a
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.

--
- 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
>
>

Apr 9 '07 #2

Bruce Wood
P: n/a
Bruce Wood
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.

Apr 9 '07 #3

=?ISO-8859-1?Q?Arne_Vajh=F8j?=
P: n/a
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
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
Apr 9 '07 #4

tylerxprice@gmail.com
P: n/a
tylerxprice@gmail.com
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.

Apr 10 '07 #5

Marc Gravell
P: n/a
Marc Gravell
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

Apr 10 '07 #6

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp c# money money c#