lya lya - 1 year ago 86
C# Question

Inconsistent Convert.ToDouble(decimal) results

I upgraded a calculation program and found the results changed unexpectedly.
I've located the problem, but have no idea how this can happen or how to solve it.

The result difference is caused by this statement


When I open two Visual Studio Community 2015 instances to run both the new and the old program, set a break point before the statement and evaluate it in the Immediate Window, I can get two results.
One is


and the other is


It seems the former is correct (4348266 rounded to 435), but if I break the former program immediately after it is started and evaluate again, the result becomes the latter.

It's not a huge difference but the behavior scares me. Which one should be the correct result? How can I avoid the problem?

Answer Source

The problem lies within what the guranteed precision of a double actually is.

According to MSDN it is 15-16 digits, and regarding your example, the difference is only visible at the 16th digit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download