sprintf

Discussion in 'Ruby' started by Michael 'entropie' Trommer, Dec 22, 2005.

  1. --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--
    Michael 'entropie' Trommer, Dec 22, 2005
    #1
    1. Advertising

  2. 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
    Florian Frank, Dec 22, 2005
    #2
    1. Advertising

  3. --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--
    Michael 'entropie' Trommer, Dec 22, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,582
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,046
  3. CJ
    Replies:
    1
    Views:
    1,309
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,818
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    536
    Pilatus
    Dec 18, 2003
Loading...

Share This Page