473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Which data type should I use for a money value?

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
5 57702
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]
- mv*@spam.guard.caspershouse.com

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:en**************@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
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
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
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Batista, Facundo | last post by:
Can't find it. I mean something like: >> m1 = Money(decimal=2) >> m2 = Money(decimal=2) >> m1.value = 3.20 >> m2.value = 2.15 >> print m1 + m2 5.35
24
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing the code. Thank you. .. Facundo
21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Jason Szotak | last post by:
Recently we upgraded the .Net Framework to v1.1. All of a sudden all of our queries run through .Net pages began showing all 4 of the decimal places for the money data type. Queries run through asp...
3
by: Ghost | last post by:
Hello. I have some problem to read Money Type Field from my database. I do so: I'm using SqlDataReader object to select some records and I have "float" type variable to store result. I write: ...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
2
by: sbcaco | last post by:
Hello again... can someone tell me how to work around this error: String or binary data would be truncated. I am doing this: update table1 set =
3
by: Stephan Diehl | last post by:
Hi lazyweb, I'm wondering, if there is a usable money data type for python available. A quick search in pypi and google didn't convey anything, even though the decimal data type seemed to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.