Skip to content

Instantly share code, notes, and snippets.

@boykoc
Created November 10, 2017 17:23
Show Gist options
  • Save boykoc/2187fbeb36c873bf42181c067d52e187 to your computer and use it in GitHub Desktop.
Save boykoc/2187fbeb36c873bf42181c067d52e187 to your computer and use it in GitHub Desktop.
Method to *evenly* split a bill among people.
##
# Module to split bills evenly for a group of people (i.e. splitting cost for a pizza lunch).
#
# Basic use:
# `BillSplitter.split_bill(total: 82.42, people: 11)` => "Each person should fork over 7.49!"
module BillSplitter
require 'bigdecimal'
def self.split_bill(total: 0.00, people: 1)
bd_total = BigDecimal.new(total.to_s)
bd_people = BigDecimal.new(people.to_s)
cost = bd_total / bd_people
"Each person should fork over #{cost.truncate(2).to_s('F')}!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment