decimal vs double

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

Apparently, financial calculations should be performed with decimal not
double, however .NET does not make it easy to work with decimal.

Literal numeric non-integral values are by default treated as doubles.

The constants in System.Math (Pi,E) are doubles.

What are the relative benefits of using decimal to store financial data ?
 
John said:
Apparently, financial calculations should be performed with decimal not
double, however .NET does not make it easy to work with decimal.

Literal numeric non-integral values are by default treated as doubles.
Use the suffix "m" to declare decimal literals.
The constants in System.Math (Pi,E) are doubles.
They're more commonly used as doubles than as decimals. If you need them
(which should be relatively rare for a financial application) it's easy
enough to declare a class yourself, though:

public static class MathD {
public const decimal PI = 3.141592653589793238462643383279m;
public const decimal E = 2.71828182845904523536028747135m;
...
}
What are the relative benefits of using decimal to store financial data ?
This page explains it better than I could:
http://www2.hursley.ibm.com/decimal/decifaq1.html

In short, floating-point calculations are subject to errors that are quickly
apparent in financial applications, which require exact representations of
exact values (as opposed to approximating everything that's not exactly
representable in binary, as floating point does). Decimals can offer this.
 

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

Back
Top