2015-01-09 09:32:40 -08:00
|
|
|
package DDG::Goodie::PercentOf;
|
2015-02-22 12:09:29 -08:00
|
|
|
# ABSTRACT: Operations with percentuals
|
2015-01-09 09:32:40 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2015-01-09 09:32:40 -08:00
|
|
|
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"];
|
2015-01-09 09:32:40 -08:00
|
|
|
|
|
|
|
my $result;
|
|
|
|
|
|
|
|
triggers query_nowhitespace => qr/\d{1,3}\%$/;
|
|
|
|
|
|
|
|
handle query_nowhitespace => sub {
|
2015-01-08 04:34:32 -08:00
|
|
|
|
2015-04-24 04:22:59 -07:00
|
|
|
return unless $_ =~ qr/^(?:\p{Currency_Symbol})*\s*(\d+\.?\d*)\s*(\+|\*|\/|\-)\s*(\d+\.?\d*)\%$/;
|
2015-01-09 09:32:40 -08:00
|
|
|
|
2015-04-23 07:45:17 -07:00
|
|
|
my $partRes = ($1 * $3) / 100;
|
|
|
|
|
2015-01-09 09:32:40 -08:00
|
|
|
if ($2 eq '-') {
|
2015-04-23 07:45:17 -07:00
|
|
|
$result = ( $1 - ( $partRes ) );
|
2015-01-09 09:32:40 -08:00
|
|
|
} elsif ($2 eq '+') {
|
2015-04-23 07:45:17 -07:00
|
|
|
$result = ( $1 + ( $partRes ) );
|
2015-01-09 09:32:40 -08:00
|
|
|
} elsif ($2 eq '*') {
|
2015-04-23 07:45:17 -07:00
|
|
|
$result = ( $partRes );
|
2015-01-09 09:32:40 -08:00
|
|
|
} elsif ($2 eq '/') {
|
2015-04-24 06:15:08 -07:00
|
|
|
$result = ( $1 * ( 100 / $3 ) );
|
2015-01-09 09:32:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
my $text = "Result: $result";
|
2015-04-23 07:45:17 -07:00
|
|
|
|
2015-01-09 09:32:40 -08:00
|
|
|
return $text,
|
|
|
|
structured_answer => {
|
2015-04-24 02:03:01 -07:00
|
|
|
input => [$_],
|
2015-01-09 09:32:40 -08:00
|
|
|
operation => 'Calculate',
|
|
|
|
result => $result
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
1;
|
|
|
|
|