# Decimal in for loop?

**URL:** <https://www.ruby-forum.com/t/decimal-in-for-loop/181308>\
**Category:** Ruby\
**Created:** [January 9, 2010, 8:04am UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308 "2010-01-09T08:04:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 8:04am UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/1 "2010-01-09T08:04:39Z")

</div>

Hi, I have a quick question: How can I increase a valuable by a certain  
decimal (not an integer) in a for loop?

For example, in C,

for( i = 0; i \< 0.5; i += 0.1 )  
printf( "%f ", i );

the code above returns

0.0 0.1 0.2 0.3 0.4

How can I write this in Ruby?

Thanks,  
Nathan

ps I looked in several Ruby tutorials, but couldn’t find any example  
using a decimal. . . Should I use another loop syntax like ‘loop’ or  
‘while/until’?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 8:40am UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/2 "2010-01-09T08:40:43Z")

</div>

Hello Nathan,

> the code above returns
> 
> 0.0 0.1 0.2 0.3 0.4
> 
> How can I write this in Ruby?

Use the step method for all descendant of Numeric class:

> > 0.step(0.5,0.1) { |i| p i }  
> > 0.0  
> > 0.1  
> > 0.2  
> > 0.3  
> > 0.4  
> > 0.5  
> > =\> 0
> 
> > 0.1.step(0.5,0.1) { |i| p i }  
> > 0.1  
> > 0.2  
> > 0.3  
> > 0.4  
> > 0.5  
> > =\> 0.1

Cheers,

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 11:56am UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/3 "2010-01-09T11:56:37Z")

</div>

Thanks a lot for the quick responding, Fleck, your suggestion works! 😉  
-Nathan

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/4 "2010-01-09T18:52:18Z")

</div>

On Sat, Jan 09, 2010 at 04:04:43PM +0900, Nathan O. wrote:

> 0.0 0.1 0.2 0.3 0.4
> 
> How can I write this in Ruby?
> 
> Thanks,  
> Nathan
> 
> ps I looked in several Ruby tutorials, but couldn’t find any example  
> using a decimal. . . Should I use another loop syntax like ‘loop’ or  
> ‘while/until’?

while or until would both work. I can think of three ways off the top  
of my head:

```
(0..0.4).step(0.1) do |f|
  p f
end

x = 0
while x < 0.5 do
  p x
  x += 0.1
end

x = 0
until x > 0.4
  p x
  x += 0.1
end

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/5 "2010-01-09T21:01:22Z")

</div>

> while or until would both work.

> ```
> x = 0
> while x < 0.5 do
> p x
> x += 0.1
> end
> 
> x = 0
> until x > 0.4
> p x
> x += 0.1
> end
> 
> ```

Yes, it is, but it looks badly long. . . As I program in Ruby, I want to  
write a code as clearly as possible.

Anyway, thanks a lot for your help, Aaron. Your suggestion makes another  
question, so please see the new thread ‘Ruby editing style rules and  
recommendation?’

Thanks in advance,  
Nathan

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [January 9, 2010, 11:15pm UTC](https://www.ruby-forum.com/t/decimal-in-for-loop/181308/6 "2010-01-09T23:15:40Z")

</div>

On 2010-01-09, Nathan O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 0.0 0.1 0.2 0.3 0.4  
> No, it doesn’t.

Nor does it print that (remember, “for” loops have no “return value” in  
C).

It might print “0.000000 0.100000 …”. 🙂

> ps I looked in several Ruby tutorials, but couldn’t find any example  
> using a decimal. . . Should I use another loop syntax like ‘loop’ or  
> ‘while/until’?

Usually, this is not the right way to do it, but you might be able to  
do something like this using one of the upto-like methods, I think  
it might be called “step”.

-s
