Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Modular syntax
Message
From
22/06/2010 11:54:32
 
 
To
22/06/2010 11:28:57
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Environment versions
Environment:
VB 9.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01470166
Message ID:
01470172
Views:
46
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform