added tips goodie

master
mattlehning 2012-08-31 17:29:35 -04:00 committed by Dylan Lloyd
parent 85cbf7cb4d
commit fcf20d85be
1 changed files with 30 additions and 0 deletions

30
lib/DDG/Goodie/Tips.pm Executable file
View File

@ -0,0 +1,30 @@
package DDG::Goodie::Tips;
use DDG::Goodie;
triggers query_lc => qr/^(\d{1,3})(?: ?%| percent) (?:(tip (?:on|for))|of) ([\$\-]?)(\d+(\.?)(?(5)\d+))$/i;
handle matches => sub {
my ($p, $is_tip, $sign,$num) = @_;
$p /= 100;
my $t = $p*$num;
my $tot;
if ($is_tip) {
$tot = $num + $t;
}
$t = sprintf "%.2f", $t;
$tot = sprintf "%.2f", $tot if $tot;
if ($tot) {
zci answer_type => 'tip';
return "Tip: \$$t Total: \$$tot";
}
$t = $sign . $t;
$tot = $sign . $tot if $tot;
zci answer_type => 'percentage';
return "$t is ".($p*100)." percent of $sign$num";
};
1;