473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 57743
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.co m

"Ronald S. Cook" <rc***@westinis .comwrote in message
news:en******** ******@TK2MSFTN GP04.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...@gma il.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
3379
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
3526
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
4526
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
6149
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
1
2276
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 still display only 2 decimal places. This is causing us some validation errors. Is there a way to configure the framework to display 2 decimal places for the money data type? Thanks, Jason
3
18484
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: myFloatVar = (float)mySqlDataReader; When this line executed the cast exception occurs.
11
3438
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 difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
2
12031
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
3440
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 planned as a money data type originaly. Thanks for any pointers Stephan
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.