Best variable type to use for money fields

B

BobRoyAce

I have a backend database that has some fields that are stored as
MONEY data type. Though this field type allows for 4 decimals, I am
only needing/wanting 2. For that reason, I am considering changing
those field types to something like DECIMAL(10,2).

My question is, which variable types are best for reading in values of
these two types? single? double?
 
A

Armin Zingler

BobRoyAce said:
I have a backend database that has some fields that are stored as
MONEY data type. Though this field type allows for 4 decimals, I am
only needing/wanting 2. For that reason, I am considering changing
those field types to something like DECIMAL(10,2).

My question is, which variable types are best for reading in values of
these two types? single? double?

You don't have a choice. If you read the data, the data is already there and
has a type determined by the data provider. If you don't know to which
Framework type the database type is mapped, just read one record and look at
the types of the values read.
Single/Double is the worst choice because you expect no rounding errors if
you work with the values.

http://msdn.microsoft.com/en-us/library/4e5xt97a.aspx


Armin
 
P

PJ6

Don't ever use floating point data types with financial data. That goes for
both the application and its database!

Also don't worry about any extra precision you get with Decimal - how many
digits are displayed is only a concern of the UI, and you can enforce a
rounding rule in the database's target table column definition. Make it a
rule and use Money as the data type so you can change your mind later if
necessary.

Paul
 
H

Herfried K. Wagner [MVP]

BobRoyAce said:
I have a backend database that has some fields that are stored as
MONEY data type. Though this field type allows for 4 decimals, I am
only needing/wanting 2. For that reason, I am considering changing
those field types to something like DECIMAL(10,2).

My question is, which variable types are best for reading in values of
these two types? single? double?

For calculations in VB code I suggest to use the 'Decimal' data type, which
does not suffer from floating point problems associated with the 'Double'
type (just read the documentation on the data types). The number of
displayed decimal places is just a matter of formatting. Take a look at the
'ToString' method of 'Decimal' to find out how to format the actual value.
 
C

Cor Ligthert[MVP]

Decimal

Double or any other floating point type cost to much time.

:)

Cor
 
M

Michel Posseth [MCP]

Okay small correction

If you do not know what you are doing only use Decimal datatypes ( maybe it
is even a good idea to change all your numeric types to decimal as i have
seen so much in code nowadays :) cause we have loads of memory and cpu
resources so why bother, and if you are wondering yes this is sarcastic )

However if you do know what you are doing you have the choice
Which Type Should I Use?
Since Decimal types are perfectly accurate and float's are not, why would we
still want to use the intrinsic float/double types? Short answer -
performance. In my speed tests Decimal types ran over 20 times slower than
their float counterparts.

So if you're writing a financial application for a bank that has to be 100%
accurate and performance is not a consideration, use the Decimal type. On
the other hand, if you need performance and extremely small floating point
variations don't affect your program, stick with the float and double types.

HTH

Michel
 

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