# Unexpected sprintf/String % Behavior

**URL:** <https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988>\
**Category:** ruby-talk\
**Created:** [15 June 2005 16:49 UTC](https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988 "2005-06-15T16:49:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Gribaudo1](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@Ben\_Gribaudo1](https://rubytalk.org/u/Ben_Gribaudo1)\
**Post date:** [15 June 2005 16:49 UTC](https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988/1 "2005-06-15T16:49:25Z")

</div>

Hello,

I am puzzled by some strange (to me) behavior exhibited by String's % method as well as sprintf. Both "%02d" % "07" and sprintf("%02d","07") output "07". However, "%02d" % "08" and sprintf("%02d","08") both raise 'ArgumentError: invalid value for Integer: "08"'. Why does a string of "07" work while "08" does not?

This is true when I test this on a MS Windows XP Pro box running Ruby 1.6.8 and when I test it on a Debian Woody box (kernal 2.4.19) running Ruby 1.8.2. IRB examples and results are below.

Thank you,  
Ben

Win XP box

> **···**
>
> -------------  
> irb(main):014:0\> "%02d" % "07"  
> =\> "07"  
> irb(main):015:0\> "%02d" % "08"  
> ArgumentError: invalid value for Integer: "08"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):15:in `%'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):15  
> irb(main):017:0\> sprintf("%02d","07")  
> =\> "07"  
> irb(main):018:0\> sprintf("%02d","08")  
> ArgumentError: invalid value for Integer: "08"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):18:in `sprintf'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):18
> 
> Linux box  
> ----------  
> irb(main):003:0\> "%02d" % "07"  
> =\> "07"  
> irb(main):004:0\> "%02d" % "08"  
> ArgumentError: invalid value for Integer: "08"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4:in `%'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4
> 
> irb(main):005:0\> sprintf("%02d","07")  
> =\> "07"  
> irb(main):006:0\> sprintf("%02d","08")  
> ArgumentError: invalid value for Integer: "08"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `sprintf'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6

---

<div class="post-metadata">

**Author:** ![Gennady1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gennady1](https://rubytalk.org/u/Gennady1)\
**Post date:** [15 June 2005 16:58 UTC](https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988/2 "2005-06-15T16:58:25Z")

</div>

Ben Gribaudo wrote:

> Hello,
> 
> I am puzzled by some strange (to me) behavior exhibited by String's % method as well as sprintf. Both "%02d" % "07" and sprintf("%02d","07") output "07". However, "%02d" % "08" and sprintf("%02d","08") both raise 'ArgumentError: invalid value for Integer: "08"'. Why does a string of "07" work while "08" does not?

Because leading 0 makes your argument string be interpreted as an octal number, where '8' is not a valid digit (0..7 are valid in base 8).

Hope it helps.  
Gennady.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [15 June 2005 16:58 UTC](https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988/3 "2005-06-15T16:58:45Z")

</div>

leading zeros (as in c) mean octal. so there is no 08. only

&nbsp;&nbsp;&nbsp;00 01 02 03 04 05 06 07

the number eight being 10

for instance

&nbsp;&nbsp;&nbsp;harp:~ \> irb  
&nbsp;&nbsp;&nbsp;irb(main):001:0\> 010  
&nbsp;&nbsp;&nbsp;=\> 8

&nbsp;&nbsp;&nbsp;irb(main):002:0\> 017  
&nbsp;&nbsp;&nbsp;=\> 15

&nbsp;&nbsp;&nbsp;irb(main):003:0\> 08  
&nbsp;&nbsp;&nbsp;SyntaxError: compile error  
&nbsp;&nbsp;&nbsp;(irb):3: Illegal octal digit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3

not that you also have hex and binary literals like

&nbsp;&nbsp;&nbsp;0xa  
&nbsp;&nbsp;&nbsp;0b101010

hth.

-a

> **···**
>
> On Thu, 16 Jun 2005, Ben Gribaudo wrote:
> 
> > Hello,
> > 
> > I am puzzled by some strange (to me) behavior exhibited by String's % method as well as sprintf. Both "%02d" % "07" and sprintf("%02d","07") output "07". However, "%02d" % "08" and sprintf("%02d","08") both raise 'ArgumentError: invalid value for Integer: "08"'. Why does a string of "07" work while "08" does not?
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Ben\_Gribaudo1](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@Ben\_Gribaudo1](https://rubytalk.org/u/Ben_Gribaudo1)\
**Post date:** [17 June 2005 19:11 UTC](https://rubytalk.org/t/unexpected-sprintf-string-behavior/18988/4 "2005-06-17T19:11:35Z")

</div>

Thank you for explaining this.

Ben
