Automator Service that does predefined simple calculations

applescriptautomator

I'm working as a salesman and what I need to do a lot of times a day is calculating the price +VAT and -VAT which is 19% here in Germany. I found some calculation automator script but wasn't able to change it to behave the way I want it to:

bc –mathlib | awk '{if($0 ~ /./) sub("\.+$","");print}'

this, ran as a shell script, takes operations so for example (somewhere) I type in the price 100*1.19, select it with the mouse and run the service. It then calculates the operation and replaces the selected text with the result 119.

But I only want to select the 100 and hit one of the services +VAT or -VAT (in the contextual menu for example) and it will be replaced by the result. That would be so awesome.

Is there anyone that can help? Maybe with a Applescript or something!
Thanks very much!

Best Answer

n = STDIN.read.scan(/-?[0-9., ]+/)[0]
separator = "."
separator = "," if n =~ /.+[. ].+,.+/ || n =~ /,/ && n !~ /\./
separator == "." ? n.gsub!(/[, ]/, "") : n = n.gsub(/[. ]/, "").gsub(",", ".")
out = "%.02f" % ((n.to_f * 1.19 * 100).round.to_f / 100)
out.gsub!(".", ",") if separator == ","
print out
  • Understands both 10,000.50 and 10.000,50, but treats 10,000 and 10.000 as 10
  • Half decimals are rounded up, negative half decimals down