# Format an integer

**URL:** <https://www.ruby-forum.com/t/format-an-integer/151893>\
**Category:** Ruby\
**Created:** [October 27, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893 "2008-10-27T16:12:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/1 "2008-10-27T16:12:03Z")

</div>

Hi,  
this is a pretty silly question but I cannot figure it how to do.  
I need to format an integer in order to have always two digits:

ex:  
1 =\> 01  
22 =\> 22

How can I do that in the shortest way possible?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/2 "2008-10-27T16:17:55Z")

</div>

On Oct 27, 2008, at 10:11 AM, Me Me wrote:

> Hi,  
> this is a pretty silly question but I cannot figure it how to do.  
> I need to format an integer in order to have always two digits:
> 
> ex:  
> 1 =\> 01  
> 22 =\> 22
> 
> How can I do that in the shortest way possible?  
> Thanks in advance

“%02d” % num

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/3 "2008-10-27T16:33:02Z")

</div>

> “%02d” % num

Thanks

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/4 "2008-10-27T16:36:50Z")

</div>

Q\_txt = res\_q[0][1]  
(0…10).each do |qt|  
question\_text = q\_txt.scan(/\w+/)[qt]  
end

when I access question\_text after, obviously it’s out of scope what am I  
missing here?

Kind Regards,  
Dan

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/5 "2008-10-27T20:13:19Z")

</div>

David A. Black wrote:

> On Tue, 28 Oct 2008, Daniel Malcolm Webb [dbw] wrote:
> 
> > Q\_txt = res\_q[0][1]  
> > (0…10).each do |qt|  
> > question\_text = q\_txt.scan(/\w+/)[qt]  
> > end
> > 
> > when I access question\_text after, obviously it’s out of scope what am I  
> > missing here?
> 
> Blocks have a kind of one-way valve local scope. Variables that  
> already exist before the block will exist in the block. Variables that  
> are created in the block do not survive past the block.

This is one reason I prefer {…} instead of do…end for blocks, since  
for me the curly braces shout “new scope”. while…end, for…end,  
until…end, if…end, unless…end do not introduce new scopes, yet  
do…end does.

Though class…end, module…end, def…end also give new scopes, there  
is little room for confusion because those are not control structures.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 27, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/6 "2008-10-27T17:36:44Z")

</div>

Hi –

On Tue, 28 Oct 2008, Daniel Malcolm Webb [dbw] wrote:

> Q\_txt = res\_q[0][1]  
> (0…10).each do |qt|  
> question\_text = q\_txt.scan(/\w+/)[qt]  
> end
> 
> when I access question\_text after, obviously it’s out of scope what am I  
> missing here?

Blocks have a kind of one-way valve local scope. Variables that  
already exist before the block will exist in the block. Variables that  
are created in the block do not survive past the block.

David

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 28, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/7 "2008-10-28T14:33:41Z")

</div>

2008/10/27 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > missing here?  
> > Not what you asked for, but: “Q\_txt” != “q\_txt”. Also, you should do  
> > the scan only once - this is more efficient:

texts = res\_q[0][1].scan(/\w+/)  
texts.each\_with\_index do |question\_text, qt|  
…  
end

> Blocks have a kind of one-way valve local scope. Variables that  
> already exist before the block will exist in the block. Variables that  
> are created in the block do not survive past the block.

Which is basically the same in many modern programming languages,  
isn’t it? Of course, there are some subtleties (e.g. whether  
shadowing of more local definitions is allowed etc.).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 28, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/8 "2008-10-28T21:30:06Z")

</div>

Robert K. wrote:

> 2008/10/27 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
> …
> 
> > Blocks have a kind of one-way valve local scope. Variables that  
> > already exist before the block will exist in the block. Variables that  
> > are created in the block do not survive past the block.
> 
> Which is basically the same in many modern programming languages,  
> isn’t it? Of course, there are some subtleties (e.g. whether  
> shadowing of more local definitions is allowed etc.).

That rules out javascript as modern. Grrr.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 28, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/format-an-integer/151893/9 "2008-10-28T14:54:51Z")

</div>

Thanks Robert,

eventually managed another work around similar to yours. The Q/q was  
indeed a typo that didn’t make it into the final code. Help is  
appreciated though 🙂

Kind Regards,  
Dan
