473,796 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using decimal type for money

Is decimal type a good choice to use for storing currency? I've got a
situation that when I store 8.50 in a decimal type variable and read it
back, I'm getting 8.5. Is there a better data type to store currency?

Thanks,
Gilgamesh
Nov 13 '05 #1
6 30032
Yes, it is intended to be used for money (currency).

You are 'getting back' "8.5" because there are built-in rules to say how a
number should be represented as a string. Look at String.Format for options.
For simple purposes, it won't matter whether you are using float, double, or
decimal, but the technicalities are that the decimal type can represent
decimal places exactly, whereas float and double types only approximate to
decimal values, so if you are doing complicated calculations then you can
get significant rounding errors if you don't use the right type.

S.

thewith the decimal type (presumably using ToStrGilgamesh
<gi*******@xter anet.com> wrote in message
news:Om******** ******@TK2MSFTN GP11.phx.gbl...
Is decimal type a good choice to use for storing currency? I've got a
situation that when I store 8.50 in a decimal type variable and read it
back, I'm getting 8.5. Is there a better data type to store currency?

Thanks,
Gilgamesh

Nov 13 '05 #2
Yes, can't find the OP now but I think it was that he was getting 8.5 back
because of using a float not a decimal, as you say decimal *happens* to
default to 2 dps (using the invariant culture anyway) but as you say you
could get 2 dps with either a float or a decimal by using String.Format.

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Simon Trew <ten.egnaro@wer ts> wrote:
Yes, it is intended to be used for money (currency).

You are 'getting back' "8.5" because there are built-in rules to say how a number should be represented as a string. Look at String.Format for
options.
I agree that using String.Format is the best way of going - but I'm
still somewhat surprised. I get 8.50 myself. For instance:

using System;

public class Test
{
public static void Main()
{
decimal d = 8.50m;

Console.WriteLi ne ("{0}", d);
}
}

prints 8.50.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #3
For me it just printed:

8.5 8.5 8.5

What did you get?

S.

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Simon Trew <ten.egnaro@wer ts> wrote:
Yes, can't find the OP now but I think it was that he was getting 8.5 back because of using a float not a decimal, as you say decimal *happens* to
default to 2 dps (using the invariant culture anyway) but as you say you
could get 2 dps with either a float or a decimal by using String.Format.


No, it's not because decimal "defaults" to 2dps - decimal stores extra
information about how many 0s there are. Try this:

using System;

public class Test
{
public static void Main()
{
Console.WriteLi ne ("{0} {1} {2}", 8.5m, 8.50m, 8.500m);
}
}

Yes, it's bizarre.

I posted some code a while ago to "normalize" a decimal, by the way.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #4
Simon Trew <ten.egnaro@wer ts> wrote:
For me it just printed:

8.5 8.5 8.5

What did you get?


8.5 8.50 8.500

Which version of .NET are you using?

If you disassemble the executable, what do you get in the IL for each
of them? I get:

IL_0005: ldc.i4.s 85
IL_0007: ldc.i4.0
IL_0008: ldc.i4.0
IL_0009: ldc.i4.0
IL_000a: ldc.i4.1
IL_000b: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0010: box [mscorlib]System.Decimal
IL_0015: ldc.i4 0x352
IL_001a: ldc.i4.0
IL_001b: ldc.i4.0
IL_001c: ldc.i4.0
IL_001d: ldc.i4.2
IL_001e: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0023: box [mscorlib]System.Decimal
IL_0028: ldc.i4 0x2134
IL_002d: ldc.i4.0
IL_002e: ldc.i4.0
IL_002f: ldc.i4.0
IL_0030: ldc.i4.3
IL_0031: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)

This was compiling with .NET 1.1, using just csc.exe.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #5
Jon,
..NET 1.1 changed the Decimal type in this regard.

..NET 1.0 will report 8.5, 1.1 will report 8.50.

Its in the change lists on www.gotdotnet.com, however that site seems to be
down just now.

Hope this helps
Jay

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Simon Trew <ten.egnaro@wer ts> wrote:
For me it just printed:

8.5 8.5 8.5

What did you get?


8.5 8.50 8.500

Which version of .NET are you using?

If you disassemble the executable, what do you get in the IL for each
of them? I get:

IL_0005: ldc.i4.s 85
IL_0007: ldc.i4.0
IL_0008: ldc.i4.0
IL_0009: ldc.i4.0
IL_000a: ldc.i4.1
IL_000b: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0010: box [mscorlib]System.Decimal
IL_0015: ldc.i4 0x352
IL_001a: ldc.i4.0
IL_001b: ldc.i4.0
IL_001c: ldc.i4.0
IL_001d: ldc.i4.2
IL_001e: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0023: box [mscorlib]System.Decimal
IL_0028: ldc.i4 0x2134
IL_002d: ldc.i4.0
IL_002e: ldc.i4.0
IL_002f: ldc.i4.0
IL_0030: ldc.i4.3
IL_0031: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)

This was compiling with .NET 1.1, using just csc.exe.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #6
I'm using 1.0, as you probably already guessed. I don't have a need to
upgrade right now.

S.

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Simon Trew <ten.egnaro@wer ts> wrote:
For me it just printed:

8.5 8.5 8.5

What did you get?


8.5 8.50 8.500

Which version of .NET are you using?

If you disassemble the executable, what do you get in the IL for each
of them? I get:

IL_0005: ldc.i4.s 85
IL_0007: ldc.i4.0
IL_0008: ldc.i4.0
IL_0009: ldc.i4.0
IL_000a: ldc.i4.1
IL_000b: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0010: box [mscorlib]System.Decimal
IL_0015: ldc.i4 0x352
IL_001a: ldc.i4.0
IL_001b: ldc.i4.0
IL_001c: ldc.i4.0
IL_001d: ldc.i4.2
IL_001e: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)
IL_0023: box [mscorlib]System.Decimal
IL_0028: ldc.i4 0x2134
IL_002d: ldc.i4.0
IL_002e: ldc.i4.0
IL_002f: ldc.i4.0
IL_0030: ldc.i4.3
IL_0031: newobj instance void [mscorlib]System.Decimal: :.ctor
(int32, int32, int32, bool, unsigned int8)

This was compiling with .NET 1.1, using just csc.exe.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #7

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

Similar topics

21
4540
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
10
5669
by: Guy Hocking | last post by:
Hi there, I have an ASP/SQL 7 application that rips data from SQL Views onto an ASP page. I have a money column in a SQL table that contains money income values. When data is input to this field it needs to be displayed as money (to 2 decimal places - 10.00 or 7.60) However, this only seems to be occurring when the user puts in the extra 0, and if 7.6 is entered, it is still displayed as 7.6
10
2131
by: teddysnips | last post by:
Application is a Work Tracking/Timesheet database. The increments of work are stored in the TimesheetItem table. This contains, inter alia, the Work Code, the Start and the Duration that the employee spent that day on a particular project. Some employees in the Network Support Department don't complete a standard 7.5 hour day for various reasons, so for every Network Support person I need to update these particular days with an amount...
11
2949
by: ZRexRider | last post by:
This may be an easy question but I've been reading for about a half hour and experimenting without results. I simply want the results of my query to display a specific field that is typed "money" using + and - The program that consumes the data expects + on positive numbers and - on negative. I was hoping to do it in the view instead of processing the results with the VB application that interogates the DB.
3
2191
by: Artek | last post by:
Hello, after installing .net 1.1, ASP.NET works fine, but there is a problems with extra zeros added to the numbers at the end. In Sql 2000 SP3a there is for example a table with one column col1. Col1 is type of money ( four digits after decimal point). Table has one row with data : col1=5.2. In .Net 1.0 the following code Response.Write("<input type=text size=3 maxlenght=4 name=test class=box value='" + dt.DefaultView + "' >");
0
2508
by: Sam | last post by:
I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data type of 'Money Type'. All I want is to able to edit/view/insert 'UnitPrice' field with 2 decimal points instead of 4 decimal points. If I use: Bind("UnitPrice", "{0:c}") in Edit Template, I got: "Input string was not in a correct format...". Although this...
4
3706
by: Ronald S. Cook | last post by:
We're designing the data model for a project. The app will be in .NET, the database will be in SQL Server 2005. I'm a little confused on type conversion between the two and which I should choose. I found this good comparison chart: http://msdn2.microsoft.com/en-us/library/ms131092.aspx But for monetary amounts, it looks like decimal is how .NET is going to
1
2837
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from the product source code, but still want to execute them under nunit. I am trying this idea on nunit sample source code. Here is my class and the experiemental code: both money.cs and Imoney.cs is compiled to cs_money.dll. then I create another...
13
10555
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am interested in what is going on behind the scenes and if there is some aspect of decimals that keep them from being used in this calculation. Thanks! Ethan Ethan Strauss Ph.D.
0
10457
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
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
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
7550
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
6792
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
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
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.