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

49 lines
1.1 KiB
Perl
Raw Normal View History

package DDG::Goodie::PercentOf;
# ABSTRACT: Operations with percentuals
use strict;
use DDG::Goodie;
zci answer_type => "percent_of";
zci is_cached => 1;
name "PercentOf";
description "Makes Operations with percentuals";
primary_example_queries "4-50%", "349*16%";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/PercentOf/PercentOf.pm";
2015-01-13 10:37:30 -08:00
attribution github => ["puskin94", "puskin"];
my $result;
triggers query_nowhitespace => qr/\d{1,3}\%$/;
handle query_nowhitespace => sub {
2015-01-08 04:34:32 -08:00
return unless $_ =~ qr/^(?:\p{Currency_Symbol})*\s*(\d+\.?\d*)\s*(\+|\*|\/|\-)\s*(\d+\.?\d*)\%$/;
2015-04-23 07:45:17 -07:00
my $partRes = ($1 * $3) / 100;
if ($2 eq '-') {
2015-04-23 07:45:17 -07:00
$result = ( $1 - ( $partRes ) );
} elsif ($2 eq '+') {
2015-04-23 07:45:17 -07:00
$result = ( $1 + ( $partRes ) );
} elsif ($2 eq '*') {
2015-04-23 07:45:17 -07:00
$result = ( $partRes );
} elsif ($2 eq '/') {
$result = ( $1 * ( 100 / $3 ) );
}
my $text = "Result: $result";
2015-04-23 07:45:17 -07:00
return $text,
structured_answer => {
2015-04-24 02:03:01 -07:00
input => [$_],
operation => 'Calculate',
result => $result
};
};
1;