>If I do this in Visual FoxPro:
>
>
>? MOD(46.90,0.05)
>
>
>I obtain 0.
>
>If I do this in .NET:
>
>
>MessageBox.Show((46.9 Mod 0.05).ToString)
>
>
>I obtain 0.049999999999996.
>
>Can someone explain me why I cannot get the same?
That must be due to the fact that the fractional part of one or both numbers cannot be exactly represented
Workarounds
(1) multiply both by 100, divide the result by 100
Console.WriteLine("{0}", ((46.9 * 100.0) % (.05 * 100.0))/100.0);
(2) do the operation in decimal type
Console.WriteLine("{0}", 46.9m % .05m);
or
Console.WriteLine("{0}", (double)((decimal)46.9 % (decimal).05));
Gregory