Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Can I pad a number with leading spaces rather than leading 0's

by greywolf (Priest)
on Aug 20, 2001 at 23:02 UTC ( [id://106313]=perlquestion: print w/replies, xml ) Need Help??

greywolf has asked for the wisdom of the Perl Monks concerning the following question:

I can use sprintf() to display numbers with leading 0's but I don't want the user to see them.

I want the users to see:
$ 60.00
$160.00
$  5.00

The $ is added in the output. I will need to sort my rows of data based on these prices.

Is there some simple way to do this? Maybe using regex to insert spaces.

mr greywolf
  • Comment on Can I pad a number with leading spaces rather than leading 0's

Replies are listed 'Best First'.
Re: Can I pad a number with leading spaces rather than leading 0's
by HyperZonk (Friar) on Aug 20, 2001 at 23:06 UTC
    The output should be right-justified (padded with spaces) automatically.

    To be more explicit: if you want the output to fit a field four spaces wide, e.g.
    $ 123.00 $ 12.00 $ 1.00
    Then you would want to
    printf("\$%7.2f\n", $output);


    -HZ
      Oops, I missed the formatting of my results list (multiple spaces are ignored yadda yadda yadda) but you nailed it. I am looking for the all $'s to line up on the left and all the decimal places to line up on the right.

      sprintf("\$%08.2f", $price) formats the way I want to but I don't want to see the 0's.

      sprintf("\$%8.2f", $price) seems to forget about the padding up front.

      mr greywolf
        sprintf("\$%8.2f", $price) seems to forget about the padding up front.
        Works fine for me:
        #!perl $price = 16.50; $formatted = sprintf("\$%8.2f", $price); print "Price is $price, formatted price is $formatted.\n"; __END__ Price is 16.5, formatted price is $ 16.50.
        Please clarify what you mean by "seems to forget." You seem to be saying that the spaces don't appear. Hmmmm ... just a thought ... are you using this to print to a web page, i.e., in HTML? You know, extra spaces don't appear in HTML unless you surround them with pre tags or similar formatting mechanisms.

        -HZ
Re: Can I pad a number with leading spaces rather than leading 0's
by TheoPetersen (Priest) on Aug 20, 2001 at 23:10 UTC
Re: Can I pad a number with leading spaces rather than leading 0's
by blakem (Monsignor) on Aug 20, 2001 at 23:10 UTC
    I've used something like this before, in little non-production scripts:
    for my $amount ('60.00','160.00','5') { print "\$", pad(dollarize($amount),6), "\n"; } sub pad { my $txt = shift; my $num = shift || 7; return " " x ($num - length($txt)) . $txt; } sub dollarize { my $txt = shift; $txt .= '.' unless $txt =~ /\./; $txt .= '0' unless $txt =~ /\.\d\d/; $txt .= '0' unless $txt =~ /\.\d\d/; return $txt; }

    -Blake

Re: Can I pad a number with leading spaces rather than leading 0's
by MZSanford (Curate) on Aug 21, 2001 at 13:40 UTC
    I think i see the problem from readin the other posts. You appear to be printing to a webpage which is not recognizing the consecutive spaces (or, atleast, not displaying them). Just for the sake of sprintf(), i should mention that , if the number before the . in the sprintf() format is positive, the number is right justified, and a negative number to the left of the . left justifies the number. so ...
    #!/usr/bin/perl -w use strict; # every job should my $number = 222.42; printf("A: \$%10.2f\n",$number); printf("B: \$%-10.2f\n",$number);
    ... would print ...
    %shell > perl test.pl A: $ 222.42 B: $222.42

    If HTML : There are quite a few ways to do it, and i have even been known to use something like the following before printing. But, i am not a CGI programmer, so i am sure there are better ways.
    sub html_space { my $Rstring = shift; $$Rstring =~ s/ / /g; }
    ... and a test program ...
    #!/usr/bin/perl -w use strict; # every job should my $number = 222.42; $number = sprintf("\$%10.2f\n",$number); &html_space(\$number); print "A: $number\n"; sub html_space { my $Rstring = shift; $$Rstring =~ s/ / /g; }

    can't sleep clawns will eat me
    -- MZSanford

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://106313]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found