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)
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.
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.
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.
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.
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
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.
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.
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...
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With