Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 ( [id://106315]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re: Re: Can I pad a number with leading spaces rather than leading 0's
by greywolf (Priest) on Aug 21, 2001 at 00:16 UTC
    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
        Thanx to everyone who responded, you guys nailed it! It was an html problem not a perl problem.

        I am now hanging my head in shame and getting several lashes with a wet noodle.

        mr greywolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found