Skip to content

Instantly share code, notes, and snippets.

@mbailey
Created December 29, 2011 04:43
Show Gist options
  • Save mbailey/1531950 to your computer and use it in GitHub Desktop.
Save mbailey/1531950 to your computer and use it in GitHub Desktop.
Should I not use ruby to add dollar values?
mbailey@oasis:~ $ irb
1.9.2p290 :001 > 120.82+15.96+135.50
=> 272.28
1.9.2p290 :002 > 120.82+15.96+135.51
=> 272.28999999999996
@mipearson
Copy link

@seancaffery: I disagree. Use a dedicated fixed-point decimal class or represent all monetary amounts internally as their smallest possible fraction. Eg, represent $100.00 as 10000, or two dollars fifty as $2.50.

If you get into a situation where you need to divide the amount, clever use of floor and ceil will ensure you have no leftovers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment