What is the best data type in C# for storing amounts

Asked By 240 points N/A Posted on -
qa-featured

I am having trouble with amount calculation in C#. I am doing an invoice program in C#. When I store a value and retrieve it back, the decimal values get rounded or an additional cent come in! I can not figure it out. I think the problem is with the data types that I am using.

SHARE
Best Answer by Stella
Answered By 0 points N/A #97223

What is the best data type in C# for storing amounts

qa-featured

What is the current data type, that you are using in your program? Is it float, decimal or double? The most recommended is to use the decimal data type for storing values such as amounts.

Answered By 240 points N/A #97224

What is the best data type in C# for storing amounts

qa-featured

I am presently using the double data type for storing values with decimals. Following is a section of my code:

double subTotal = 0.0;

subTotal = 10020.71

/// code to store the data to the database

//code to retrieve

Text1.Test = subTotal

The decimal value 71 becomes 73 cents!  Why do you say that Decimal data type is more suited?

Answered By 0 points N/A #97225

What is the best data type in C# for storing amounts

qa-featured

In C#, the Decimal data type can store "absolute" values. The double and float data types can store the whole part accurately. But when it comes to the decimal portion of a value ( the "cents" part), it goes to the floating point portion. I. e. the value is stored as a floating point number and not as the absolute value. That is an approximation of the value is stored. 

Depending on the database you are using and the platform, the floating point will differ. Sometimes due to memory mappings, a double value when stored, may introduce a 0.0000000001 value in its decimal portion, causing unprecedented problems when used for money calculations. Change the data type to Decimal and your problem should be solved!

Answered By 240 points N/A #97226

What is the best data type in C# for storing amounts

qa-featured

Stella,

I tried your suggestion. I changed all the "double" to the decimal data type. I did a few test runs on different values. It reduced the problem but did not actually eliminate it! Any ideas as to what could be causing this?

Answered By 0 points N/A #97227

What is the best data type in C# for storing amounts

qa-featured

What is the database you are using? It might be a problem with the data type you have chosen in the underlying database table. If you can tell me the database type, I can help you.

Answered By 240 points N/A #97228

What is the best data type in C# for storing amounts

qa-featured

The database I am using is MySQL. I am connecting it with the ODBC connector provided for .Net. The data type in the database is "float".

Best Answer
Best Answer
Answered By 0 points N/A #97229

What is the best data type in C# for storing amounts

qa-featured

You need to define the number of decimal places in the column. If you just specified it as a "float", MySQL will allocate the maximum length for the data type and will shift the decimal point depending on the value (hence the term "floating point").

If you fix the number of decimals in a consistent fashion, you will not get this problem. That is, if your application limits and rounds the decimal portion to 4 places, you need to set the column's decimal portion to the same number of slots.

Answered By 240 points N/A #97231

What is the best data type in C# for storing amounts

qa-featured

Thank you Stella! Your solution worked! Now the values are coming correctly!

Answered By 0 points N/A #97232

What is the best data type in C# for storing amounts

qa-featured

Glad to be of help! Have a nice day!

Login/Register to Answer

Related Questions