# Sprintf() bug

**URL:** <https://rubytalk.org/t/sprintf-bug/4678>\
**Category:** ruby-talk\
**Created:** [12 February 2003 18:25 UTC](https://rubytalk.org/t/sprintf-bug/4678 "2003-02-12T18:25:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Warren\_Brown3](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@Warren\_Brown3](https://rubytalk.org/u/Warren_Brown3)\
**Post date:** [12 February 2003 18:25 UTC](https://rubytalk.org/t/sprintf-bug/4678/1 "2003-02-12T18:25:45Z")

</div>

I have run across a discrepancy between the documentation and ruby  
1.6.8. According to the pickaxe book, the flag character ‘0’ changes  
padding from spaces (’ ') to zeros (‘0’). This works for numbers, but does  
not work for stings:

irb(main):001:0\> sprintf “%05d,%05s”, 1, ‘1’  
=\> “00001, 1”

```
A simple fix (from the 1.6.8 version) would be:

```

> **···**
>
> * * *
> 
> — sprintf.c.old Thu Dec 12 03:16:44 2002  
> +++ sprintf.c.new Wed Feb 12 11:12:07 2003  
> @@ -287,8 +287,9 @@  
> CHECK(width);  
> width -= len;  
> if (!(flags&FMINUS)) {
> 
> - 
> 
> ```
> char c = (flags & FZERO) ? '0' : ' ';
> while (width--) {
> 
> ```
> 
> - 
> 
> ```
> buf[blen++] = ' ';
> 
> ```
> 
> - 
> 
> ```
> buf[blen++] = c;
> }
> }
> 
> ```
> 
> * * *
> 
> ```
> To get around this problem, I ended up using the combination
> 
> ```
> 
> mystring.rjust(5).tr(’ ',‘0’). This worked for me since my string was  
> guaranteed not to have any spaces, but if that were not the case, this would  
> get messy.
> 
> ```
> It occurred to me that String#ljust and String#rjust really seem like
> 
> ```
> 
> they should have an optional second parameter that defaults to ’ ', but  
> could be any character (or any string?). Anyone else think this is a good  
> idea?
> 
> ```
> - Warren Brown
> 
> ```

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [12 February 2003 19:04 UTC](https://rubytalk.org/t/sprintf-bug/4678/2 "2003-02-12T19:04:51Z")

</div>

> ```
> I have run across a discrepancy between the documentation and ruby
> 
> ```
> 
> 1.6.8. According to the pickaxe book, the flag character ‘0’ changes  
> padding from spaces (’ ') to zeros (‘0’). This works for numbers, but does  
> not work for stings:

man printf

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.  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

i think the pickaxe is simply incorrect - the printf function just delegates  
to stdlib printf after all.

> ```
> To get around this problem, I ended up using the combination
> 
> ```
> 
> mystring.rjust(5).tr(’ ',‘0’). This worked for me since my string was  
> guaranteed not to have any spaces, but if that were not the case, this would  
> get messy.
> 
> ```
> It occurred to me that String#ljust and String#rjust really seem like
> 
> ```
> 
> they should have an optional second parameter that defaults to ’ ', but  
> could be any character (or any string?). Anyone else think this is a good  
> idea?

agreed.

-a

> **···**
>
> On Thu, 13 Feb 2003, Warren Brown wrote:
> 
> –
> 
> ====================================
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ahoward@fsl.noaa.gov](mailto:ahoward@fsl.noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ====================================

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [12 February 2003 23:14 UTC](https://rubytalk.org/t/sprintf-bug/4678/3 "2003-02-12T23:14:41Z")

</div>

Hi,

> I have run across a discrepancy between the documentation and ruby  
> 1.6.8. According to the pickaxe book, the flag character ‘0’ changes  
> padding from spaces (’ ') to zeros (‘0’). This works for numbers, but does  
> not work for stings:

Pickaxe is great, but not perfect (nor me).

> It occurred to me that String#ljust and String#rjust really seem like  
> they should have an optional second parameter that defaults to ’ ', but  
> could be any character (or any string?). Anyone else think this is a good  
> idea?

This sounds nice. In fact, I have a plan to do so.

```
						matz.

```

> **···**
>
> In message “sprintf() bug” on 03/02/13, “Warren Brown” [wkb@airmail.net](mailto:wkb@airmail.net) writes:
