# Sprintf bug (?)

**URL:** <https://www.ruby-forum.com/t/sprintf-bug/75635>\
**Category:** Ruby\
**Created:** [November 11, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635 "2006-11-11T19:30:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 11, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/1 "2006-11-11T19:30:15Z")

</div>

Hello all,

I am wondering if I have just found a bug in (s)printf…

According to the PickAxe, table “sprintf flag characters”:

================= snip ==================================

0 (zero) all Pad with zeros, not spaces.

================= snip ==================================

so I think this call

irb(main):053:0\> sprintf(“%05s”, 123)  
=\> " 123"

should correctly result in

“00123”

or I am getting something wrong?

thanks,  
Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 11, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/2 "2006-11-11T19:50:18Z")

</div>

Peter S. wrote:

> ================= snip ==================================  
> or I am getting something wrong?  
> I think ruby follows the C standard, which says zero padding only  
> applies to numeric formats.

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 12, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/3 "2006-11-12T22:21:20Z")

</div>

----- Original Message -----  
From: “Peter S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Saturday, November 11, 2006 1:28 PM  
Subject: sprintf bug (?)

> ================= snip ==================================
> 
> so I think this call
> 
> irb(main):053:0\> sprintf(“%05s”, 123)  
> =\> " 123"
> 
> should correctly result in
> 
> “00123”

sprintf(“%05d”,123) # ‘d’ not ‘s’  
=\> “00123”

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 12, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/4 "2006-11-12T22:45:28Z")

</div>

Bernard K. wrote:

> > irb(main):053:0\> sprintf(“%05s”, 123)  
> > =\> " 123"
> > 
> > should correctly result in
> > 
> > “00123”
> 
> sprintf(“%05d”,123) # ‘d’ not ‘s’  
> =\> “00123”

🙂 I know ‘d’ is not ‘s’, it was a bad example. It should have been

irb(main):002:0\> sprintf(“%10s”,‘hello’)  
=\> " hello"

(there are no zeroes…)

Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 13, 2006, 12:35am UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/5 "2006-11-13T00:35:38Z")

</div>

Hi,

In message “Re: sprintf bug (?)”  
on Mon, 13 Nov 2006 07:03:44 +0900, Timothy H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Possibly you meant “%010s” instead of “%10s”? In any case, I checked the  
|output of a C program compiled with gcc on my Powerbook running OS X  
|10.8. In this case, the ‘0’ flag adds padding on the left:

This is platform dependent. Linux manpage says:

```
   0 The value should be zero padded. For d, i, o, u, x, X,
          a, A, e, E, f, F, g, and G conversions, the converted
          value is padded on the left with zeros rather than
          blanks. If the 0 and - flags both appear, the 0 flag is
          ignored. If a precision is given with a numeric
          conversion (d, i, o, u, x, and X), the 0 flag is
          ignored. For other conversions, the behavior is
                                           ~~~~~~~~~~~~~~~
          undefined.
          ~~~~~~~~~

```

zero option affects some specifiers. %s is not among them.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 13, 2006, 8:06am UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/6 "2006-11-13T08:06:09Z")

</div>

Yukihiro M. wrote:

> zero option affects some specifiers. %s is not among them.

Thank, Matz. This made it really clear at last…

Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 12, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/7 "2006-11-12T23:05:29Z")

</div>

Peter S. wrote:

> > > so I think this call  
> > > =\> “00123”

Possibly you meant “%010s” instead of “%10s”? In any case, I checked the  
output of a C program compiled with gcc on my Powerbook running OS X  
10.8. In this case, the ‘0’ flag adds padding on the left:

ruby$ cat testit.c  
#include \<stdio.h\>

int main(int argc, char \*argv[])  
{  
printf("%010s", “hello\n”);  
return 0;  
}

ruby$ ./testit  
0000hello  
ruby$

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 14, 2006, 2:45am UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/8 "2006-11-14T02:45:27Z")

</div>

----- Original Message -----  
From: “Yukihiro M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 12, 2006 6:34 PM  
Subject: Re: sprintf bug (?)

> This is platform dependent. Linux manpage says:  
> ~~~~~~~~~
> 
> zero option affects some specifiers. %s is not among them.
> 
> matz.

I suggest that the OP asks ruby’s description of sprintf via “ri  
sprintf”  
… it is essentially as described above

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [November 14, 2006, 2:19am UTC](https://www.ruby-forum.com/t/sprintf-bug/75635/9 "2006-11-14T02:19:24Z")

</div>

----- Original Message -----  
From: “Peter S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Sunday, November 12, 2006 4:43 PM  
Subject: Re: sprintf bug (?)

> > > so I think this call
> 
> 🙂 I know ‘d’ is not ‘s’, it was a bad example. It should have been
> 
> irb(main):002:0\> sprintf(“%10s”,‘hello’)  
> =\> " hello"
> 
> (there are no zeroes…)
> 
> Peter

This is the correct behavior of sprintf for strings
