# Sprintf

**URL:** <https://www.ruby-forum.com/t/sprintf/74269>\
**Category:** Ruby\
**Created:** [December 22, 2005, 1:27am UTC](https://www.ruby-forum.com/t/sprintf/74269 "2005-12-22T01:27:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 22, 2005, 1:27am UTC](https://www.ruby-forum.com/t/sprintf/74269/1 "2005-12-22T01:27:24Z")

</div>

Hello,

according ti the ruby core docu Kernel#sprintf:

> For string fields, the precision determines the maximum number of  
> characters to be copied from the string. (Thus, the format sequence  
> %10.10s will always contribute exactly ten characters to the result.)

my irb:

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?

## So long

Michael ‘entropie’ Trommer; [http://ackro.org](http://ackro.org)

ruby -e “0.upto((a=‘njduspAhnbjm/dpn’).size-1){|x| a[x]-=1}; p  
‘mailto:’+a”

---

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 22, 2005, 1:45am UTC](https://www.ruby-forum.com/t/sprintf/74269/2 "2005-12-22T01:45:28Z")

</div>

Michael ‘entropie’ Trommer wrote:

> 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?

You didn’t use a precision:

sprintf("%10.2s", “lalaa”) # =\> " la"

This creates a size 10 string, left padded with spaces, and copies only  
two characters from the argument.

sprintf("%10.2f", 1212.123) # =\> " 1212.12"

This creates a size 10 string, left padded with spaces, and displays the  
floating point number with two decimal places.

---

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 22, 2005, 1:51am UTC](https://www.ruby-forum.com/t/sprintf/74269/3 "2005-12-22T01:51:29Z")

</div>

- Florian F. (removed\_email\_address@domain.invalid) wrote:

> Michael ‘entropie’ Trommer wrote:
> 
> > What s my mistake?

> You didn’t use a precision:
> 
> sprintf(“%10.2s”, “lalaa”) # =\> " la"  
> This creates a size 10 string, left padded with spaces, and displays the  
> floating point number with two decimal places.

thanks alot.

## So long

Michael ‘entropie’ Trommer; [http://ackro.org](http://ackro.org)

ruby -e “0.upto((a=‘njduspAhnbjm/dpn’).size-1){|x| a[x]-=1}; p  
‘mailto:’+a”
