From fcf20d85bed22cc37e4b824f57043da0fa6d9919 Mon Sep 17 00:00:00 2001 From: mattlehning Date: Fri, 31 Aug 2012 17:29:35 -0400 Subject: [PATCH] added tips goodie --- lib/DDG/Goodie/Tips.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 lib/DDG/Goodie/Tips.pm 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;