Using decimal type for money

G

Gilgamesh

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
 
S

Simon Trew

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
 
S

Simon Trew

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

Jon Skeet

Simon Trew said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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 said:
Simon Trew said:
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.
 
S

Simon Trew

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

S.

Jon Skeet said:
Simon Trew said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top