Last active
April 11, 2017 15:51
-
-
Save ryenski/3a3bcb6a3e0e8de0171db07eaa0f6da2 to your computer and use it in GitHub Desktop.
A simple way to deal with monetary amounts in cents, without using a library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# amount_cents :integer default("0") | |
def amount=(amount) | |
amount = amount.gsub(/[^\d\.]/, '').to_f if amount.is_a?(String) | |
self.amount_cents = amount * 100 | |
end | |
def amount | |
amount_cents / 100.0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
amount_cents should default to
0
or an exception may be raised.