Does this work for you? I know it is probably just a problem with type comparison but it still seems a little stupid.VB Code:
For i = 0.2 To 0.3 Step 0.01 If i = 0.25 Then Form1.Print "0.25 - Yay! :)" Else Form1.Print i End If Next i
Printable View
Does this work for you? I know it is probably just a problem with type comparison but it still seems a little stupid.VB Code:
For i = 0.2 To 0.3 Step 0.01 If i = 0.25 Then Form1.Print "0.25 - Yay! :)" Else Form1.Print i End If Next i
What do you mean by it acting funny? All I saw was that it never got to .25 and therefore didn't print it...
It works this way:
VB Code:
Private Sub Form_Load() Dim i As Single For i = 0.2 To 0.3 Step 0.01 If Val(i) = 0.25 Then Debug.Print "0.25 - Yay! " Else Debug.Print i End If Next i End Sub
Try this with the Val() function:
VB Code:
For i = 0.2 To 0.3 Step 0.01 If Val(i) = 0.25 Then Form1.Print "0.25 - Yay! :)" Else Form1.Print i End If Next
[edit]
Dammit... a day late and a dollar short... :(
beat ya' ;)Quote:
Originally posted by MidgetsBro
Try this with the Val() function:
VB Code:
For i = 0.2 To 0.3 Step 0.01 If Val(i) = 0.25 Then Form1.Print "0.25 - Yay! :)" Else Form1.Print i End If Next
[edit]
Dammit... a day late and a dollar short... :(
Anyway... have a look at this thread Why is this false?
Is the loop variable a variant, a double, or a ???
It doesn't mind much really....
I managed to fix the problem straight away but for someone who is new to VB that could cause a really annoying problem.
I read the other thread and in this case i is probably something like 0.2500000000000001, indeed, if you increase the 0.3 to 1.0, the code prints 0.810000000000001 instead of 0.81. The print code must require 13 or more 0 before it cuts them off.
Maybe the = function should work this way as well, or an alternative provided?
Oh well :)
Adding Val() does not fix all the instances of this problem:In this case you need to round() it to 2 decimal places.Code:Dim i As Double
For i = 0.1 To 1 Step 0.02
If Val(i) = 0.94 Then
Form1.Print "0.94 - Yay! "
Else
Form1.Print i
End If
Next i