I have the following code:
Dim x As Double = 45.333
Dim y As Double = 45
Dim z As Double = x - y
Me.Label1.Text = z.ToString()
The result should obviously be 0.333, but the value displayed in
Me.Label1.Text is:
0.332999999999998
I have seen stuff like this in other situations, but I never managed to
figure out how to fix it. Can somebody help me here?

Signature
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/
Teemu - 12 Jan 2008 20:12 GMT
>I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?
In order to show the correct result you have to format it. For example you
could do this:
Me.Label1.Text =z.ToString("0.000")
Doubles are floating point numbers and that causes this behavior.
If you want to find more information try keyword "IEEE 754" with Google.
-Teemu
Family Tree Mike - 12 Jan 2008 22:08 GMT
Consider using decimal instead of double if you want to get the answer you
expect below.
> I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?
Erik Funkenbusch - 13 Jan 2008 03:01 GMT
> I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?
Double is doing entirely correct subtraction.
The problem is that Double is an implementation of IEEE floating point, and
IEEE floating point is incapable of representing all numbers exactly.
Read this:
http://support.microsoft.com/kb/42980
Cor Ligthert[MVP] - 13 Jan 2008 06:09 GMT
Nathan,
This is basic
Float, Double or whatever floating poing calculation for mathimatical
programming,
Decimal for business programming,
Cor
Herfried K. Wagner [MVP] - 13 Jan 2008 12:32 GMT
"Nathan Sokalski" <njsokalski@hotmail.com> schrieb:
> Dim x As Double = 45.333
> Dim y As Double = 45
[quoted text clipped - 8 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?
In addition to the other replies, take a look at these articles:
IEEE Standard 754 Floating Point Numbers
<URL:http://steve.hollasch.net/cgindex/coding/ieeefloat.html>

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>