Skip to content

Instantly share code, notes, and snippets.

@msimpson msimpson/tip.rb
Created Aug 20, 2011

Embed
What would you like to do?
Tip Calculator
#!/usr/bin/env ruby
# Tip Calculator
# Usage: ./tip <cost> <minutes_waited> [percent]
exit if ARGV.length < 2
cost = ARGV[0].to_f.round(2)
minutes = ARGV[1].to_i
percent = !ARGV[2].nil? ? ARGV[2].to_f / 100.0 : 0.2
max = cost / 40 * 60
scale = ( Math.cos( minutes * Math::PI / (120 * ( cost / 41 ))) * cost * percent )
tip = ( minutes >= max ? 0 : minutes < 5 ? cost - (cost / 5 * minutes) : scale ).round(2)
def pad(n)
return n < 10 ? "0#{n}" : n
end
printf(
"Cost: $%.02f, Wait: %d:%s, Tip: $%0.2f\n",
cost, minutes / 60.floor, pad(minutes % 60), tip
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.