# Sprintf: what does mean the percent symbol?

**URL:** <https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094>\
**Category:** Ruby\
**Created:** [April 28, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094 "2008-04-28T21:50:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 28, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/1 "2008-04-28T21:50:04Z")

</div>

Hi all!

I’m testing the functionality of sprintf and I know that I can use it in  
two way:

sprintf("%.3f", “1.2345”)  
=\> “1.234”

Or

“%.3f” % “1.2345”  
=\> “1.234”

In the second example, what does mean the percent symbol (%) between  
“%.3f” and “1.2345”? It means apply format string “%.3f” to string  
“1.2345”? What I want to know is, the percent symbol is limited to  
sprintf or it has a more general use in ruby?

I’ve searched on the ruby pickaxe and several other ruby books but I  
haven’t found anything on the % alone, obviously there are many use of  
that combined with option (array: %w, string: %q,%Q, regexp: %r, …).

Thanks.

Best regards.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 28, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/2 "2008-04-28T22:11:42Z")

</div>

On Mon, Apr 28, 2008 at 9:50 PM, Toki T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “%.3f” % “1.2345”  
> =\> “1.234”
> 
> In the second example, what does mean the percent symbol (%) between  
> “%.3f” and “1.2345”? It means apply format string “%.3f” to string  
> “1.2345”? What I want to know is, the percent symbol is limited to  
> sprintf or it has a more general use in ruby?

% is a clever trick to define operator % (as in modulo, i.e. 7 % 3 ==

1. on strings  
to apply formatting. The same trick is used in python, and even in C++  
(boost::format).  
You’ll find more details at  
[http://ruby-doc.org/core/classes/String.html#M000785](http://ruby-doc.org/core/classes/String.html#M000785)

> I’ve searched on the ruby pickaxe and several other ruby books but I  
> haven’t found anything on the % alone, obviously there are many use of  
> that combined with option (array: %w, string: %q,%Q, regexp: %r, …).

This is another %.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 28, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/3 "2008-04-28T22:13:29Z")

</div>

On 28.04.2008 21:50, Toki T. wrote:

> Hi all!
> 
> I’m testing the functionality of sprintf and I know that I can use it in  
> two way:
> 
> sprintf(“%.3f”, “1.2345”)  
> =\> “1.234”

Although it works the way you did it, that should have read

sprintf(“%.3f”, 1.2345)

IOW, it’s a float format and not a string format.

> Or
> 
> “%.3f” % “1.2345”  
> =\> “1.234”

Same here:

“%.3f” % 1.2345

Actually you can do

irb(main):001:0\> “%3d %04d” % [1,2]  
=\> " 1 0002"

i.e. work with multiple arguments. But for that I prefer (s)printf.

> In the second example, what does mean the percent symbol (%) between  
> “%.3f” and “1.2345”? It means apply format string “%.3f” to string  
> “1.2345”? What I want to know is, the percent symbol is limited to  
> sprintf or it has a more general use in ruby?

% is an operator and is implemented / overloaded for several classes.  
The most commonly used is probably modulus operation for Fixnums.

irb(main):003:0\> 10 % 3  
=\> 1

> I’ve searched on the ruby pickaxe and several other ruby books but I  
> haven’t found anything on the % alone, obviously there are many use of  
> that combined with option (array: %w, string: %q,%Q, regexp: %r, …).

That’s a completely different story, here % I would not call “operator”  
because it is not runtime effective. This occurrence of the percent  
sign is evaluated at compile time, more precisely during parsing. All  
these are convenience syntaxes to more easily express certain literals.  
For example, if you have a regular expression containing forward  
slashes the %r form is much easier on the eye:

%r{/+} vs. //+/

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 28, 2008, 10:14pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/4 "2008-04-28T22:14:42Z")

</div>

Hi –

On Tue, 29 Apr 2008, Toki T. wrote:

> “%.3f” % “1.2345”  
> =\> “1.234”
> 
> In the second example, what does mean the percent symbol (%) between  
> “%.3f” and “1.2345”? It means apply format string “%.3f” to string  
> “1.2345”? What I want to know is, the percent symbol is limited to  
> sprintf or it has a more general use in ruby?
> 
> I’ve searched on the ruby pickaxe and several other ruby books but I  
> haven’t found anything on the % alone, obviously there are many use of  
> that combined with option (array: %w, string: %q,%Q, regexp: %r, …).

You can define a % method, which will then be executed when you use  
the infix %.

David

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 29, 2008, 8:35am UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/5 "2008-04-29T08:35:23Z")

</div>

Robert K. wrote:

> it’s a float format and not a string format.
> 
> > Or
> > 
> > “%.3f” % “1.2345”  
> > =\> “1.234”
> 
> Same here:
> 
> “%.3f” % 1.2345

According to pickaxe2, p. 606, “%” is the name of an instance method in  
the String class, and its syntax is:

str % arg

The return value is a string.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 29, 2008, 11:23am UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/6 "2008-04-29T11:23:12Z")

</div>

2008/4/29 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > “%.3f” % 1.2345
> 
> According to pickaxe2, p. 606, “%” is the name of an instance method in  
> the String class, and its syntax is:
> 
> str % arg
> 
> The return value is a string.

I’m not sure I get your point. Can you elaborate?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 29, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/7 "2008-04-29T13:17:05Z")

</div>

Hi –

On Tue, 29 Apr 2008, Robert K. wrote:

> %r{/+} vs. //+/

But not the brain 🙂 I know there’s no consensus about these things,  
but I’ll just put in a word for //. Having trained my brain to  
understand //+/ at a glance, I always find it much harder to  
understand things like {/+}, and similarly with /x-style regexes,  
which I always have to slow down to read.

Chacun Ã son cerveau 🙂

David

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 30, 2008, 12:44am UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/8 "2008-04-30T00:44:40Z")

</div>

Thanks to all for the help and fast answers.

So back to my two example:

sprintf("%.3f", “1.2345”)  
=\> “1.234”

Or

“%.3f” % “1.2345”  
=\> “1.234”

Are they the same thing written in different way or the first use  
sprintf and the second use str % arg, both giving the same result?

Thanks.

Best regards.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [April 30, 2008, 9:58am UTC](https://www.ruby-forum.com/t/sprintf-what-does-mean-the-percent-symbol/136094/9 "2008-04-30T09:58:04Z")

</div>

2008/4/29 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> if you have a regular expression containing forward slashes the %r form is  
> much easier on the eye:
> 
> > %r{/+} vs. //+/
> 
> But not the brain 🙂 I know there’s no consensus about these things,

Yeah, my initial reaction was “maybe not for _your_ brain”. 🙂

> but I’ll just put in a word for //. Having trained my brain to  
> understand //+/ at a glance, I always find it much harder to  
> understand things like {/+}, and similarly with /x-style regexes,  
> which I always have to slow down to read.

I for my part find longish regular expressions much, much more  
readable and understandable if they are pulled apart with /x probably  
across multiple lines and with comments.

> Chacun à son cerveau 🙂

I’m glad babelfish can help my brain decipher this. :-)))

Kind regards

robert
