|
-
Sep 24th, 2002, 07:10 PM
#1
Thread Starter
Addicted Member
Hmmm...VB being funny?
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
Does this work for you? I know it is probably just a problem with type comparison but it still seems a little stupid.
What is the answer to this question?
-
Sep 24th, 2002, 07:16 PM
#2
PowerPoster
What do you mean by it acting funny? All I saw was that it never got to .25 and therefore didn't print it...
-
Sep 24th, 2002, 07:18 PM
#3
Need-a-life Member
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
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Sep 24th, 2002, 07:19 PM
#4
PowerPoster
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...
-
Sep 24th, 2002, 07:21 PM
#5
Need-a-life Member
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...
beat ya' 
Anyway... have a look at this thread Why is this false?
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Sep 24th, 2002, 07:28 PM
#6
New Member
Is the loop variable a variant, a double, or a ???
-
Sep 24th, 2002, 07:44 PM
#7
-
Sep 25th, 2002, 03:05 AM
#8
Thread Starter
Addicted Member
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
What is the answer to this question?
-
Sep 25th, 2002, 03:12 AM
#9
Thread Starter
Addicted Member
Adding Val() does not fix all the instances of this problem:
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
In this case you need to round() it to 2 decimal places.
What is the answer to this question?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|