I will be working for a company where they are programming for Financial Institutions and I will be working with money a lot. Before that it wasn’t a major concern for me because I’ve been doing small money things and Double was enough at some point but now even 1 penny is darn important.
I think everyone may know the
Dim mValue as Decimal = 100 'seems the best type for now
mValue = mValue / 3
Console.WriteLine(mValue)
Console.ReadLine()
mValue = mValue * 3
Console.WriteLine(mValue)
'Outputs 99.9999999999999999999999999... and never hits 100
So how can I get over this problem and find even better precise results?
Thanks.
I’m not an expert in this field so someone with more experience might answer this better.
You can pretty safely round to 10000 digets or so, and then round to the nearest penny when you want to use the number. This works for the example you posted
100/ 3 = 33.333333333333333 * 3 = 99.99999999999 which is then rounded to 100.
I know that many things use whats called bankers’ rounding. This is to prevent losing money when something ends with exactly 5. It rounds up if the number before the 5 is odd, and it rounds down if the number is even. Something to consider.
http://en.wikipedia.org/wiki/Rounding