Results 1 to 14 of 14

Thread: [RESOLVED] Need a sanity check - calculating percentages in a loop

Threaded View

  1. #1

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Resolved [RESOLVED] Need a sanity check - calculating percentages in a loop

    First up, the code:
    vb.net Code:
    1. 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]
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width