The solution is to format it in two passes. In pass 1 you map your data to what you want ie 1,234.00 or '' for a 0 result using sprintf, a commify sub, and some logic. In your printf you simply render this data in the desired columns using %Ns for all data. Perl will happily stringify the formatted numbers. You could also use a format

You might SuperSearch for commify for numerous ways to add commas, but be aware not all of them deal with floats correctly. Something along these lines should do the trick:

printf "%-30s %11ss %7s%% %10s %16s\n", $_, $ela{$_} ? commify( sprintf( "%8.2f", $ela{$_}/$version ) ) + : '', $ela{$_} ? commify( sprintf( "%5.1f", $ela{$_}/$r*100) ) + : '', $ctr{$_} ? commify( sprintf( "%10d", $ctr{$_} ) ) + : '', $ctr{$_} && $ela{$_} ? sprintf( "%8.7f", $ela{$_}/$ctr{$_}/$versio +n ): ''; sub commify { local $_ = reverse shift; /\./g; s/\G(\d{3})(\d)/$1,$2/g; scalar reverse $_ }

You need to allow for potential extra field width from the commas of course.

cheers

tachyon


In reply to Re: printf format to blank by tachyon
in thread printf format to blank by Lhamo Latso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.