Results 1 to 3 of 3

Thread: Looping with decimal values

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    2

    Looping with decimal values

    Hello all,

    I'm taking visual basic programming via an online course for my Computer Science degree. I have some past programming experience in other languages, but I'm just diving into VB for the first time. I observed something in my programming assignments this week that may be common knowledge to more experienced VB programmers, and I was hoping to get a bit of insight on the "why" of what I'm seeing. Here's what I observed in a nutshell:

    Both with Do/While and Do/Until Loops and For/Next loops, I've noticed that, when using decimal values the loop does not execute a final time when it reaches the terminating value, but with integer values it does. Here's an example:

    Dim n As Integer = 1
    Do
    ListBox1.Items.Add((n).ToString)
    n = n + 1
    Loop Until n > 20

    This puts the numbers 1 through 20 into the list box.

    If I do the same thing, but with decimal values as shown below:

    Dim n As Double = 0.01
    Do
    ListBox1.Items.Add((n).ToString("P0"))
    n = n + 0.01
    Loop Until n > 0.2

    I would expect the list box to contain items ranging 1% to 20%, instead the last item in the box is 19%

    As I mentioned, the same is true for For/Next loops

    For n = 0.01 to 0.2 Step 0.01
    ListBox1.Items.Add((n).ToString("P0"))
    Next

    Give 1% to 19% instead of 1% to 20%.

    So, I'm hoping someone might be able to shine some light on this for me. I noticed, to that if I don't use ToString and just output the values directly into the list box, some of them look like 0.16000000 or 0.12999999

    So, I'm betting it is something with how VB handles decimals in equations, but know the exact reason why will help me know when and how to account for this with more complex code. Thanks in advance for any help you're able to provide.

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    2

    Re: Looping with decimal values

    I should note, I did search online and within these forums for an answer and didn't find one. It may be the way I'm wording the search so if this question was already posed and answered, I apologize.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Looping with decimal values

    Double is a floating-point type and is not capable of representing every value exactly. Some values are only approximate and that's what's happening in your code. That's why it is recommended to use Decimal rather than Double where absolute accuracy is required, e.g. financial calculations. Double is much more efficient in use, which is why it is used by default, but Decimal doesn't suffer from such round-off issues. This behaviour of floating-point numbers is well-documented.

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