# Sprintf

**URL:** <https://rubytalk.org/t/sprintf/23512>\
**Category:** ruby-talk\
**Created:** [22 December 2005 00:40 UTC](https://rubytalk.org/t/sprintf/23512 "2005-12-22T00:40:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Sheppard](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Daniel\_Sheppard](https://rubytalk.org/u/Daniel_Sheppard)\
**Post date:** [22 December 2005 00:40 UTC](https://rubytalk.org/t/sprintf/23512/1 "2005-12-22T00:40:18Z")

</div>

> irb(main):004:0\> sprintf("%2s", "lalaa")  
> =\> "lalaa"  
> irb(main):005:0\> sprintf("%2i", 1212)  
> =\> "1212"
> 
> ruby 1.8.3 (2005-09-21) [i686-linux]
> 
> What s my mistake?

Reading comprehension? =)

sprintf("%2.2s","lalaa")  
&nbsp;&nbsp;&nbsp;&nbsp;==\>"la"

You can't do the equivalent for a number (as the precision setting for a  
number specifies decimal places).

> **···**
>
> #####################################################################################  
> This email has been scanned by MailMarshal, an email content filter.  
> #####################################################################################  
> #####################################################################################  
> This e-mail message has been scanned for Viruses and Content and cleared  
> by NetIQ MailMarshal  
> #####################################################################################

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [22 December 2005 01:37 UTC](https://rubytalk.org/t/sprintf/23512/2 "2005-12-22T01:37:49Z")

</div>

> irb(main):004:0\> sprintf("%2s", "lalaa")  
> =\> "lalaa"  
> irb(main):005:0\> sprintf("%2i", 1212)  
> =\> "1212"
> 
> What s my mistake?

Daniel Sheppard wrote:

> sprintf("%2.2s","lalaa")  
> &nbsp;&nbsp;&nbsp;&nbsp;==\>"la"
> 
> u can't do the equivalent for a number (as the precision setting for a  
> number specifies decimal places).

You \_can\_ do the equivalent for a number two ways that I can think of:

sprintf("%i", 1212 % 10\*\*2) #=\> "12" # modular arithmetic to truncate high  
digits  
sprintf("%.2s", 1212) #=\> "12" # cast integer to string

Also, I usually like using String#% instead of sprintf, just in case you're  
not familiar with it:

"%.2s" % 1212 #=\> "12"

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Michael\_entropie\_Tro](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Michael\_entropie\_Tro](https://rubytalk.org/u/Michael_entropie_Tro)\
**Post date:** [22 December 2005 01:43 UTC](https://rubytalk.org/t/sprintf/23512/3 "2005-12-22T01:43:10Z")

</div>

i wasnt familiar with it, quite impressive!

So long

> **···**
>
> \* Dave Burt (dave@burt.id.au) wrote:
> 
> > Also, I usually like using String#% instead of sprintf, just in case you're  
> > not familiar with it:
> > 
> > "%.2s" % 1212 #=\> "12"
> 
> --  
> Michael 'entropie' Trommer; [http://ackro.org](http://ackro.org)
> 
> ruby -e "0.upto((a='njduspAhnbjm/dpn').size-1){|x| a-=1}; p 'mailto:'+a"
