6 Replies - 5575 Views - Last Post: 03 September 2010 - 01:02 PM Rate Topic: -----

#1 shongololo1983  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 10:07 AM

Good day all

I have to loop through a range of numbers starting with 300.0000 and ending with 301.9999

then do a check with some sql code to see if each number in the loop exist.

The problem I am having is that I was using

 For i As Integer = 1 To 300


Before and it worked well with whole numbers but when I use

 For i As Integer = 300.0000 To 301.9999


i becomes 300 (drops the trailing zero's and now loops 300, 301 instead of 300.0000,300.0001, 300.0002


Does anyone have any idea how I can force my code to keep the 0's after the "." or another way perhaps to achieve my goal?
Please bear in mind I am a bit of a newby still.

Thanks
Shongololo

Is This A Good Question/Topic? 0
  • +

Replies To: How to Loop through a range of decimal numbers ?

#2 trip2137  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 28
  • Joined: 28-May 10

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 10:10 AM

Integer doesn't allow decimals!

You should use another datatype.

edit: link removed - was C++ ;-)

This post has been edited by trip2137: 03 September 2010 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 10:18 AM

like double
For i As Double=300 to 301.9999
'stuff here'
Next i



the bad(or good thing) is that VB.NET will round them and will count from 300 to 302 if you use integer for i. if that was C# an error will show up biting your head of for using integer with decimal numbers

This post has been edited by NoBrain: 03 September 2010 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 10:59 AM

By default the stepping size is 1, so need to adjust it.
For i As Double=300 To 301.9999 Step 0.0001

'stuff here'

Next i



Was This Post Helpful? 0
  • +
  • -

#5 shongololo1983  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 12:11 PM

Thanks all that helped especially the step 0.0001

Still have the problem that I need to check if 300.000 exists in the DB. But the zero's after the dot gets dropped so now it's looking for 300 instead of 300.000 is there anyway to retain the zero's after the dot?
Was This Post Helpful? 0
  • +
  • -

#6 Somyagupta  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 85
  • Joined: 05-August 10

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 12:40 PM

you can enter your numbers as strings and tell the compiler to convert them, like this:

Dim dec1 As Decimal
dec1 = Convert.ToDecimal("100.00", _
Globalization.CultureInfo.GetCultureInfo("en-us"))




Basically convert string to decimal.



Hope this helps

This post has been edited by Somyagupta: 03 September 2010 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 shongololo1983  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Re: How to Loop through a range of decimal numbers ?

Posted 03 September 2010 - 01:02 PM

Ha! Got it figured out thanks guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1