Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created March 21, 2012 15:41
Show Gist options
  • Save luckyruby/2148676 to your computer and use it in GitHub Desktop.
Save luckyruby/2148676 to your computer and use it in GitHub Desktop.
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