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!


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
  • WOW, that was fast! But it does only recognize 99.99 not 99,99. Is it possible, to get it a) read with comma and b) replaces out with comma? (best way would be output like input!) – Thron Jan 31 '12 at 14:24
  • @Thron You're right, I edited the script... It should now round numbers correctly as well. – Lri Jan 31 '12 at 17:05
  • I wish I could upvote this question and answer more than once. Services are such an amazing feature of Mac OS X, and this use case illustrates why. – Daniel Jan 31 '12 at 17:09
  • This is so unbelievable awesome... Thank you so much! – Thron Jan 31 '12 at 17:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .