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";