zeroclickinfo-goodies/lib/DDG/Goodie/SigFigs.pm

40 lines
946 B
Perl

package DDG::Goodie::SigFigs;
use DDG::Goodie;
triggers start => 'sigfigs', 'sigdigs', 'sf', 'sd', 'significant';
zci answer_type => "sig_figs";
zci is_cached => 1;
handle remainder => sub {
$_ =~ s/^(figures|digits)\s*//g;
return unless /^-?\d+(?:\.(?:\d+)?)?$/;
$_ =~ s/-//;
$_ =~ s/^0+//;
my @arr = split('\\.', $_);
my $v = @arr;
my $len = 0;
# there's a decimal
unless ($v eq 1) {
# the string doesn't have integers on the left
# this means we can strip the leading zeros on the right
if ($_ < 1) {
$arr[1] =~ s/^0+//;
$len = length $arr[1];
}
#there are integers on the left
else {
$len = length($arr[0]) + length($arr[1]);
}
}
# no decimal
else {
# lose the trailing zeros and count
$_ =~ s/\.?0*$//;
$len = length $_;
}
return "Significant figures: $len";
};
1;