Is there an equivalent of the decimal type in D?

Jeff Nowakowski jeff at dilacero.org
Sat Jan 27 12:20:36 PST 2007


Sean Kelly wrote:
> Unless you're simply dealing with numbers too large to be represented 
> exactly with floating-point (16 digits for 64-bit double, and I can't 
> remember the limit for 80-bit real offhand), I think it makes more sense 
> to use floating point internally and round to the appropriate degree of 
> precision for display.

Floating point is a very bad idea for representing money.  If you're 
adding up currencies then you want an exact representation, not "close 
enough".  Sooner or later you will get bitten by rounding error if you 
use floats.

> The other consideration would be if the 
> financial spec required rounding to specific degrees of precision at 
> various points in the calculation, though rounding with floating-point 
> is still obviously an option.

A very bad option.  If the interest rate is 0.0239 or whatever, then 
that is what you should use, not the "close enough" floating point.  You 
then use Banker's Rounding whenever you adjust an account.

-Jeff


More information about the Digitalmars-d-learn mailing list