Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

  1. As requested, a non-sprintf solution:
    $percent = int(100 * $one_thirtieth) . '%';
  2. Think about what rounding behavior you need; if $f = 888/1000, then:
    int(100*$f) will truncate to 88,
    sprintf(  '%d',100*$f) will truncate to 88,
    sprintf('%.0f',100*$f) will round to 89.
  3. You can merge formatting and concatenations into a single sprintf call:
    ok( $keyword_density >= $min_keyword_density, sprintf( 'Keyword density %.0f%%, minimum %.0f%%', 100 * $keyword_density, 100 * $min_keyword_density, ) );

Here is the code I was testing with:

use strict; use warnings; use Test::More; my @in_out = ( [ 1, 3 => '33%' ], [ 1, 30 => '3%' ], # [ 888, 1000 => '88%' ], # Use if we want truncation # [ -888, 1000 => '-88%' ], # Use if we want truncation [ 888, 1000 => '89%' ], # Use if we want rounding [ -888, 1000 => '-89%' ], # Use if we want rounding ); my @code = ( [ f1 => sub { int(100 * $_[0]) . '%' } ], [ f2 => sub { sprintf '%02d%%', 100 * $_[0] } ], [ f3 => sub { sprintf '%2d%%', 100 * $_[0] } ], [ f4 => sub { sprintf '%d%%', 100 * $_[0] } ], [ f5 => sub { sprintf '%.0f%%', 100 * $_[0] } ], ); plan tests => @in_out * @code; foreach (@code) { my ($name, $sub) = @$_; foreach (@in_out) { my ($numer, $denom, $expected) = @$_; my $frac = $numer / $denom; my $percent = $sub->($frac); is($percent, $expected, "$name($numer/$denom)"); # print "$name '$numer/$denom'\t'$expected'\t'$percent'\n"; } print "\n"; }


In reply to Re^4: sprintf percent formatting by Util
in thread sprintf percent formatting by tphyahoo

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-24 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found