printf doesn't do right justify

Discussion in 'Perl Misc' started by Daniel Cutter, Mar 9, 2008.

  1. I thought that printf would allways right justify numbers, unless told
    to do differently (with '-'). perlfunc doesn't tell.

    #!perl
    use strict;
    use warnings;
    printf("%2.1f\n", 12.3);
    printf("%2.1f\n", 4.5);
    printf("%02.1f\n", 6.7);

    The results are:
    12.3
    4.5
    6.7

    But I expected:
    12.3
    4.5
    06.7

    What am I missing?

    perl -v says:
    This is perl, v5.10.0 built for MSWin32-x86-multi-thread
    It's Win XP with all the patches

    Regards
    Daniel Cutter
     
    Daniel Cutter, Mar 9, 2008
    #1
    1. Advertising

  2. Daniel Cutter

    Ben Morrow Guest

    Quoth Daniel Cutter <_IS_DE>:
    > I thought that printf would allways right justify numbers, unless told
    > to do differently (with '-'). perlfunc doesn't tell.
    >
    > #!perl
    > use strict;
    > use warnings;
    > printf("%2.1f\n", 12.3);
    > printf("%2.1f\n", 4.5);
    > printf("%02.1f\n", 6.7);


    The number before the . is the minimum total field width, not the number
    of places before the decimal (yes, I think this is confusing too, but
    it's more consistent with %3.5s). So you want

    printf("%4.1f\n", 12.3);
    printf("%4.1f\n", 4.5);
    printf("%04.1f\n", 6.7);

    Ben
     
    Ben Morrow, Mar 9, 2008
    #2
    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. Tina

    Right Justify Hyperlink Buttons?

    Tina, Dec 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    604
    G Dean Blake
    Dec 9, 2004
  2. John Wilson

    right justify a column?

    John Wilson, Jun 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    665
    John Wilson
    Jun 22, 2005
  3. John Wilson

    Right justify text in textbox in web page

    John Wilson, Oct 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,504
    John Wilson
    Oct 8, 2005
  4. How to right justify a column

    , Jul 23, 2003, in forum: C Programming
    Replies:
    2
    Views:
    10,578
  5. printf: Centre justify data

    , Dec 2, 2005, in forum: C Programming
    Replies:
    2
    Views:
    355
Loading...

Share This Page