# How to Loop through a range of decimal numbers ?

Page 1 of 1

## 6 Replies - 8784 Views - Last Post: 03 September 2010 - 01:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=188955&amp;s=13011f45f4a2599021951c7242edc07f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shongololo1983

Reputation: 0
• 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

Reputation: 3
• 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

### #3 raziel_

• Like a lollipop

Reputation: 469
• Posts: 4,280
• 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

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• 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

```

### #5 shongololo1983

Reputation: 0
• 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?

### #6 Somyagupta

Reputation: 6
• 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

### #7 shongololo1983

Reputation: 0
• 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!