Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

sprintf percent formatting

by tphyahoo (Vicar)
on Aug 17, 2005 at 12:31 UTC ( [id://484393]=perlquestion: print w/replies, xml ) Need Help??

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

I basically want input 1/30, output is "Result: 3%".

Currently I'm doing it as below, which works for 33% but not for 3% (extra zero at the beginning, i.e. 03%). Is there a more elegant/intuitive way to do this than just using sprintf?

Thanks!

use strict; use warnings; use Test::More qw(no_plan); my $one_third = 1/3; my $percent = sprintf("%02d%%",$one_third*100); is($percent,"33%"); print "Result: $percent\n"; my $one_thirtieth = 1/30; $percent = sprintf("%02d%%",$one_thirtieth*100); is($percent,"3%"); print "Result: $percent\n";

Replies are listed 'Best First'.
Re: sprintf percent formatting
by tbone1 (Monsignor) on Aug 17, 2005 at 12:50 UTC
    Change the sprintf statements to use "%2d%%" instead of "%02d%%". The leading 0 forces left-padding of 0s on numbers.

    --
    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

Re: sprintf percent formatting
by b10m (Vicar) on Aug 17, 2005 at 12:50 UTC

    sprintf is probably what you want, yet give it a slightly altered input and it'll work:

    use strict; use warnings; use Test::More qw(no_plan); my $percent = sprintf("%d%%",(1/3)*100); is($percent,"33%"); print "Result: $percent\n"; $percent = sprintf("%d%%",(1/30)*100); is($percent,"3%"); print "Result: $percent\n";

    Instead of '%02d%%', I now use '%d%%' which seems to be what you want.

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: sprintf percent formatting
by derby (Abbot) on Aug 17, 2005 at 12:49 UTC
    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

      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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found