Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > sprintf

Reply
Thread Tools

sprintf

 
 
Michael 'entropie' Trommer
Guest
Posts: n/a
 
      12-22-2005
--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+sAHZIY P4gUv0xQCcDwij
YuRTSSXU83CxnzSidlAKUYE=
=i78u
-----END PGP SIGNATURE-----

--BOKacYhQ+x31HxR3--


 
Reply With Quote
 
 
 
 
Florian Frank
Guest
Posts: n/a
 
      12-22-2005
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.

--
Florian Frank



 
Reply With Quote
 
 
 
 
Michael 'entropie' Trommer
Guest
Posts: n/a
 
      12-22-2005
--fUYQa+Pmc3FrFX/N
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Florian Frank () wrote:
> Michael 'entropie' Trommer wrote:
> >What s my mistake?


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a function similar to C++'s sprintf Pep Java 5 08-25-2005 08:23 PM
sprintf shea martin Java 5 09-03-2004 01:40 AM
difficulties with sprintf function Pilatus C++ 3 12-18-2003 07:00 PM
atoi: stringstream or old C sprintf function Mike Chirico C++ 2 11-19-2003 03:59 PM
String class, how to implement sprintf? CJ C++ 1 10-28-2003 10:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57