Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Line up cols using printf %m.nf

by pgmer6809 (Sexton)
on Feb 19, 2021 at 02:21 UTC ( [id://11128548]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to write a report and have the decimal points in the various numbers line up properly. But using say %4.2f or % 4.2f or %04.2f all have the same problem when the value in question is zero. Is there a format spec that will fix this?

 perl -e'$fmt="\n%3d : %04.2f : %3d";printf $fmt,3,12.34,-1;printf $fmt,2,00.00,1;print "\n";'

Result

3 : 12.34 : -1 2 : 0.00 : 1

Replies are listed 'Best First'.
Re: Line up cols using printf %m.nf
by pryrt (Abbot) on Feb 19, 2021 at 02:37 UTC
    12.34 has five characters, not four, so to line up, you need %5.2f

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found