Skip to content

Instantly share code, notes, and snippets.

@semmons99
Created April 19, 2011 14:04
Show Gist options
  • Save semmons99/927927 to your computer and use it in GitHub Desktop.
Save semmons99/927927 to your computer and use it in GitHub Desktop.
def bankers_round(amount)
scaled_amt = amount * BigDecimal("100")
if scaled_amt.frac > BigDecimal("0.5")
return scaled_amt.ceil / BigDecimal("100")
elsif scaled_amt.frac < BigDecimal("0.5")
return scaled_amt.floor / BigDecimal("100")
end
return scaled_amt.floor / BigDecimal("100") if scaled_amt.to_i.even?
scaled_amt.ceil / BigDecimal("100")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment