|
-
Jun 15th, 2017, 09:32 AM
#1
[RESOLVED] Need a sanity check - calculating percentages in a loop
First up, the code:
vb.net Code:
If ((rowCount / totalRows) * 100) Mod 10 = 0 Then
Now for the explanation:
I've got a loop where I'm processing a file line by line. Through the process, I want to give some feed back to the user. I want to do it on each 10% (10,20,30, etc)
totalRows is the number of rows in the file. rowCount is the current row I'm about to process.
Obviously rowCount/totalRows gives back the decimal value, which I then multiple by 100 get the percent value. I then mod it by 10 and see if it is 0, and if it is, I update the user with a message.
Seems straight forward, right? Problem is I'm getting 20%, 40%, 60%, 80%, and 100%, I'm missing 10, 30, 50, 70 & 90...
I think it might be because I don't get an exact 10%... it might be doing something like 9.98, followed by 11.1 ... so it skips 10%
I've tried putting a cint around the left portion before the modulus operation... that didn't work, I've tried flipping the / for \ that didn't help...
What am I missing?
-tg
pre-posting edit - ok, I confirmed that the problem is that I'm not hitting perfect % on the odd ones... if I hard code totalRows to 100, then I get 10, 20, 30,... as expected... so ... hmmm.... now I don't know what to do. Is there a way to do a "slop" range?
Last edited by dday9; Jun 15th, 2017 at 09:39 AM.
Reason: Replaced [/code] with [/highlight]
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
|