Search code examples
c#visual-studiodoubledivisionmodulo

Modulo C# is not producing expected value


I try to check whether 2 doubles are divisible, so I can conclude whether a given point is on a certain grid or not. I use the % operator, but I seem to do something wrong because the output is not what I expect.

In this example I try to divide 5 by 0.2, the result should be 25.0, but the modulo operator doesn't show this to me. As can be seen in the image;

double LUpDown.Value = 20
double nUpDown.Value = 100
double stepx = LUpDown.Value / double nUpDown.Value = 0.2
double xvalp = 5

then when I compute the % operator things go wrong;

xvalp % stepx = 0.1999999999..... ??

I expect a 0 because 5/0.2 = 25.0

enter image description here


Solution

  • Can you try using decimal instead of double ?