# Number formatting

**URL:** <https://rubytalk.org/t/number-formatting/38003>\
**Category:** ruby-talk\
**Created:** [31 May 2007 16:03 UTC](https://rubytalk.org/t/number-formatting/38003 "2007-05-31T16:03:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cory](https://avatars.discourse-cdn.com/v4/letter/c/898d66/32.png) [@Cory](https://rubytalk.org/u/Cory)\
**Post date:** [31 May 2007 16:03 UTC](https://rubytalk.org/t/number-formatting/38003/1 "2007-05-31T16:03:26Z")

</div>

folks - can't quite figure this one out...

foo = 5  
bar = 10

basically - when i go to print out the numbers - i want to format 'foo'  
with a leding zero so that it reads '05' and is aesthetically pleasing  
when compared to a double-digit number - such as bar: '10'.

essentially looking to get:

05  
10  
25  
03

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [31 May 2007 16:09 UTC](https://rubytalk.org/t/number-formatting/38003/2 "2007-05-31T16:09:57Z")

</div>

foo = 5  
puts "%02d" % foo

--------------------------------------------------------------- String#%  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str % arg =\> new\_str

> **···**
>
> On Fri, Jun 01, 2007 at 01:03:26AM +0900, Cory wrote:
> 
> > folks - can't quite figure this one out...
> > 
> > foo = 5  
> > bar = 10
> > 
> > basically - when i go to print out the numbers - i want to format 'foo'  
> > with a leding zero so that it reads '05' and is aesthetically pleasing  
> > when compared to a double-digit number - such as bar: '10'.
> > 
> > essentially looking to get:
> > 
> > 05  
> > 10  
> > 25  
> > 03
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Format---Uses \_str\_ as a format specification, and returns the  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result of applying it to \_arg\_. If the format specification  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contains more than one substitution, then \_arg\_ must be an +Array+  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;containing the values to be substituted. See +Kernel::sprintf+ for  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;details of the format string.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%05d" % 123 #=\> "00123"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"%-5s: %08x" % ["ID", self.id] #=\> "ID : 200e14d6"

---

<div class="post-metadata">

**Author:** ![Ball\_Donald\_A\_Jr\_Lib](https://avatars.discourse-cdn.com/v4/letter/b/f475e1/32.png) [@Ball\_Donald\_A\_Jr\_Lib](https://rubytalk.org/u/Ball_Donald_A_Jr_Lib)\
**Post date:** [31 May 2007 16:11 UTC](https://rubytalk.org/t/number-formatting/38003/3 "2007-05-31T16:11:33Z")

</div>

> folks - can't quite figure this one out...
> 
> foo = 5  
> bar = 10
> 
> basically - when i go to print out the numbers - i want to  
> format 'foo'  
> with a leding zero so that it reads '05' and is aesthetically  
> pleasing when compared to a double-digit number - such as bar: '10'.

sprintf('%02d', foo)

- donald

---

<div class="post-metadata">

**Author:** ![Luis1](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@Luis1](https://rubytalk.org/u/Luis1)\
**Post date:** [31 May 2007 16:13 UTC](https://rubytalk.org/t/number-formatting/38003/4 "2007-05-31T16:13:44Z")

</div>

Try using Kernel#printf.

irb(main):001:0\> printf("%02d", 5)  
05=\> nil

> **···**
>
> On 5/31/07, Cory \<coryw@americanmonkey.com\> wrote:
> 
> > folks - can't quite figure this one out...
> > 
> > foo = 5  
> > bar = 10
> > 
> > basically - when i go to print out the numbers - i want to format 'foo'  
> > with a leding zero so that it reads '05' and is aesthetically pleasing  
> > when compared to a double-digit number - such as bar: '10'.
> 
> --  
> Luis Parravicini  
> [http://ktulu.com.ar/blog/](http://ktulu.com.ar/blog/)
