diff --git a/lib/DDG/Goodie/Tips.pm b/lib/DDG/Goodie/Tips.pm new file mode 100755 index 000000000..4a4328826 --- /dev/null +++ b/lib/DDG/Goodie/Tips.pm @@ -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;