Created
August 20, 2011 03:03
-
-
Save msimpson/1158586 to your computer and use it in GitHub Desktop.
Tip Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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