473,412 Members | 2,003 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,412 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 57705
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.