Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: sprintf percent formatting

by derby (Abbot)
on Aug 17, 2005 at 12:49 UTC ( [id://484403]=note: print w/replies, xml ) Need Help??


in reply to sprintf percent formatting

How about printf and dropping the 0.
use strict; use warnings; my $one_third = 1/3; printf( "Result: %2d%%\n", $one_third * 100 ); my $one_thirtieth = 1/30; printf( "Result: %2d%%\n", $one_thirtieth * 100 );
-derby

Replies are listed 'Best First'.
Re^2: sprintf percent formatting
by xdg (Monsignor) on Aug 17, 2005 at 13:14 UTC

    How about just specifying decimal precision on the sprintf:

    > perl -e 'print sprintf("%.0f%", 1/30*100)' 3%

    Update: Aside from sprintf formatting, if what you're really trying to do is compare two floating point numbers for Test::More, you might want to look at Test::Number::Delta, which I wrote to avoid that kind of roundabout way of comparing numbers. E.g.

    use Test::More tests => 1; use Test::Number::Delta within => .01; my $percent = 1/30; delta_ok( $percent, 0.03, 'values within 1%');

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: sprintf percent formatting
by Roger (Parson) on Aug 17, 2005 at 12:59 UTC
    Well, I don't believe the OP wants to drop the leading 0. The OP wants to know if there is a more elegant way to write a test case without using sprintf. Correct me if I am wrong.

      No no, I was happy with all three answers about dropping the 0. In fact, it was a testing situation that led me to post this question, but I am okay with my current Test::More solution, which winds up along the lines of
      ......code..... ok($keyword_density >= $min_keyword_density,"Keyword density " . sprin +tf("%2d%%",$keyword_density*100) . ", minimum " . sprintf("%2d%%",$mi +n_keyword_density*100) );
      This seems elegant enough for me, but as always I welcome suggestions.

        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:

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found