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

31 lines
672 B
Perl
Raw Normal View History

2012-08-31 14:29:35 -07:00
package DDG::Goodie::Tips;
use DDG::Goodie;
2012-09-29 16:19:39 -07:00
triggers any => 'tip', 'tips', '%';
2012-08-31 14:29:35 -07:00
2012-09-29 16:19:39 -07:00
handle query_lc => sub {
return unless my ($p, $is_tip, $sign,$num) = $_ =~/^(\d{1,3})(?: ?%| percent) (?:(tip (?:on|for|of))|of)(?: an?)? ([\$\-]?)(\d+(\.?)(?(5)\d+))(?: bill)?$/;
2012-08-31 14:29:35 -07:00
$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';
2012-10-01 11:24:27 -07:00
return "Tip: \$$t; Total: \$$tot";
2012-08-31 14:29:35 -07:00
}
$t = $sign . $t;
$tot = $sign . $tot if $tot;
zci answer_type => 'percentage';
return "$t is ".($p*100)." percent of $sign$num";
};
1;