Created
March 21, 2012 15:41
-
-
Save luckyruby/2148676 to your computer and use it in GitHub Desktop.
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
require 'bigdecimal' | |
#define rates | |
tax_rate = 0.07 | |
tip_rate = 0.18 | |
answer = {:total => 190} #initialize known round total | |
for subtotal in (1 .. 150).map(&:to_s) | |
subtotal = BigDecimal.new(subtotal) | |
total = (subtotal*(1+BigDecimal.new(tax_rate.to_s)+BigDecimal.new(tip_rate.to_s))*100.0).round/100.0 | |
if subtotal == subtotal.round && total == total.round && total < answer[:total] | |
answer[:subtotal] = subtotal | |
answer[:total] = total | |
end | |
end | |
puts "subtotal: #{"%.2f" % answer[:subtotal]}" | |
puts "total: #{"%.2f" % answer[:total]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment