'vb.net divistion wrong outcome

A very weird issue happens when substracting two numbers in vb.net It returns a wrongful outcome for a very simple math equation : I am using visual studio 2019 I don't want to use any math.round to fix the value for such simple equation Any ideas why that issue happens?

Dim diff As Decimal = 100.1 - 100
MsgBox(diff)

it returns 0.0999999999999943

 MsgBox(diff)



Solution 1:[1]

There is a lot more going on in your math than you assume! You have three different primitive types there:

  • 100.1 is a Double
  • 100 is an Integer
  • diff is a Decimal

If you put Option Strict On at the top of your code, your code will not compile. This will tell you

Option Strict On disallows implicit conversions from 'Double' to 'Decimal'.

So you must either explicitly cast (this is what your Option Strict Off version is doing implicitly)

Dim diff As Decimal = CDec(100.1 - 100)

but since it's the same thing you are already doing implicitly, you will get that same floating point issue. The better thing to do is to start with a Decimal instead of Double in the first place, such as

Dim diff = 100.1D - 100

By using the Decimal literal D at the end of the floating point number it actually does Decimal math, and the result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 djv