Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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
share|improve this answer
    
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. –  Lauri Ranta 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 Lawson Jan 31 '12 at 17:09
    
This is so unbelievable awesome... Thank you so much! –  Thron Jan 31 '12 at 17:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.