sprintf

  • Thread starter Michael 'entropie' Trommer
  • Start date
M

Michael 'entropie' Trommer

--BOKacYhQ+x31HxR3
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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

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

--BOKacYhQ+x31HxR3
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDqfJVBBd8ye5RguQRAiXKAJ9KZe96DvU8vH7+sAHZIYP4gUv0xQCcDwij
YuRTSSXU83CxnzSidlAKUYE=
=i78u
-----END PGP SIGNATURE-----

--BOKacYhQ+x31HxR3--
 
F

Florian Frank

Michael said:
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.
 
M

Michael 'entropie' Trommer

--fUYQa+Pmc3FrFX/N
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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

thanks alot.

So long
--
Michael 'entropie' Trommer; http://ackro.org

ruby -e "0.upto((a=3D'njduspAhnbjm/dpn').size-1){|x| a[x]-=3D1}; p 'mailto:=
'+a"

--fUYQa+Pmc3FrFX/N
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDqfgsBBd8ye5RguQRAtRTAJ0aUQPkzc1168t++MAxmO/YRdz+3gCfSarB
FqUTXNfe2BSIJ+FliVeDfgI=
=NYiE
-----END PGP SIGNATURE-----

--fUYQa+Pmc3FrFX/N--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

howto tab completion 2
cool way to save big object tree 0
state pattern? 4
printf with octal numbers 1
alarm? 4
Mini-library: SOCKSify all TCPSocket instances 0
using common layout in Rails 3
FileUtils.cp 4

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top