# Number formatting

**URL:** <https://www.ruby-forum.com/t/number-formatting/105609>\
**Category:** Ruby\
**Created:** [May 31, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/number-formatting/105609 "2007-05-31T18:03:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/898d66/32.png) [@cory](https://www.ruby-forum.com/u/cory)\
**Post date:** [May 31, 2007, 6:03pm UTC](https://www.ruby-forum.com/t/number-formatting/105609/1 "2007-05-31T18: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

---

<div class="post-metadata">

**Author:** ![cory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/898d66/32.png) [@cory](https://www.ruby-forum.com/u/cory)\
**Post date:** [May 31, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/number-formatting/105609/2 "2007-05-31T18:10:39Z")

</div>

On Fri, Jun 01, 2007 at 01:03:26AM +0900, Cory wrote:

> essentially looking to get:
> 
> 05  
> 10  
> 25  
> 03

foo = 5  
puts “%02d” % foo

## --------------------------------------------------------------- String#% str % arg =\> new\_str

```
 Format---Uses _str_ as a format specification, and returns the
 result of applying it to _arg_. If the format specification
 contains more than one substitution, then _arg_ must be an +Array+
 containing the values to be substituted. See +Kernel::sprintf+ for
 details of the format string.

    "%05d" % 123 #=> "00123"
    "%-5s: %08x" % ["ID", self.id] #=> "ID : 200e14d6"
```

---

<div class="post-metadata">

**Author:** ![cory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/898d66/32.png) [@cory](https://www.ruby-forum.com/u/cory)\
**Post date:** [May 31, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/number-formatting/105609/3 "2007-05-31T18:12:39Z")

</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:** ![cory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/898d66/32.png) [@cory](https://www.ruby-forum.com/u/cory)\
**Post date:** [May 31, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/number-formatting/105609/4 "2007-05-31T18:14:39Z")

</div>

On 5/31/07, Cory [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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’.

Try using Kernel#printf.

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