Advertisement
Guest User

Split The Bill

a guest
Nov 30th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cffunction name="splitbill" output="false" >
  2.    
  3.     <!--- Define the arguments  --->
  4.     <cfargument name="amount" required="yes" type="numeric">
  5.     <cfargument name="people" required="yes" type="numeric">
  6.    
  7.     <!--- Set vars from the arguments, converting them to absolute values to handle
  8.           negative numbers --->
  9.     <cfset people = Abs(arguments.people)>
  10.     <cfset amount = Abs(arguments.amount)>
  11.    
  12.     <!--- Convert the dollar amount to cents --->
  13.     <cfset amount = amount * 100>
  14.    
  15.     <!--- Find the whole-cent amount for all but one person to pay --->
  16.     <cfset spliteach = amount \ people>
  17.    
  18.     <!--- Find the aditional whole-cent amount for one person to pay --->
  19.     <cfset splitextera = amount % people>
  20.    
  21.     <!--- Loop to add the base amount to each persons check --->
  22.     <cfloop from="1" to="#people#" index="i">
  23.         <cfset checkamount[#i#] = spliteach>
  24.     </cfloop>
  25.    
  26.     <!--- Select who to add additional to --->
  27.     <cfset additional = #RandRange(1, people, "SHA1PRNG")#>
  28.    
  29.     <!--- Add additional --->
  30.     <cfset checkamount[#additional#] = checkamount[#additional#] + splitextera>
  31.    
  32.     <!--- Convert back to dollars --->
  33.     <cfloop from="1" to="#arraylen(checkamount)#" index="i">
  34.         <cfset checkamount[#i#] = checkamount[#i#] /100>
  35.     </cfloop>
  36.    
  37.     <!--- Return value --->
  38.     <cfreturn checkamount>
  39. </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement