Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a double really unsuitable for money?

I always tell in c# a variable of type double is not suitable for money. All weird things could happen. But I can't seem to create an example to demonstrate some of these issues. Can anyone provide such an example?

(edit; this post was originally tagged C#; some replies refer to specific details of decimal, which therefore means System.Decimal).

(edit 2: I was specific asking for some c# code, so I don't think this is language agnostic only)

like image 935
doekman Avatar asked Nov 25 '08 08:11

doekman


People also ask

Why double is not good for money?

All floating point values that can represent a currency amount (in dollars and cents) cannot be stored exactly as it is in the memory. So, if we want to store 0.1 dollars (10 cents), float/double can not store it as it is.

How accurate is a double?

A double which is usually implemented with IEEE 754 will be accurate to between 15 and 17 decimal digits. Anything past that can't be trusted, even if you can make the compiler display it.

Should price be float or double?

Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float. For example, to store the annual salary of the CEO of a company, double will be a more accurate choice.

Why should you not use float for money?

When doing any kind of calculation with currency, accuracy is extremely important. And floating point numbers (floats and doubles) don't have an accurate enough representation to prevent rounding errors from accumulating when doing arithmetic with monetary values.

Is it possible to Double Your Money?

That said, doubling your money is a realistic goal that an investor should always aim for. Broadly speaking, there are five ways to get there. The method you choose depends largely on your appetite for risk and your timeline for investing. 1. The Classic Way—Earning It Slowly

How to calculate does money double every 7 years?

Where in the above formula to determine does money double every 7 years: With 6 percent ROI: You get 6 % return if you want to know the time by which your money doubles you will divide 72 by 6 percent as follows: Thus, by the 12 years, your money will get double with a 6 percent return on your investment.

Is Double Your Money a realistic goal?

It's a badge of honor dragged out at cocktail parties and a promise made by overzealous advisors. Perhaps it comes from deep in our investor psychology – the risk-taking part of us that loves the quick buck. That said, doubling your money is a realistic goal that an investor should always aim for.

How long does it take for an investment to double?

It won't double in a year, but it should, eventually, given the old rule of 72. The rule of 72 is a famous shortcut for calculating how long it will take for an investment to double if its growth...


1 Answers

Very, very unsuitable. Use decimal.

double x = 3.65, y = 0.05, z = 3.7; Console.WriteLine((x + y) == z); // false 

(example from Jon's page here - recommended reading ;-p)

like image 133
Marc Gravell Avatar answered Sep 19 '22 15:09

Marc Gravell