Skip to content

Instantly share code, notes, and snippets.

@ryenski
Last active April 11, 2017 15:51
Show Gist options
  • Save ryenski/3a3bcb6a3e0e8de0171db07eaa0f6da2 to your computer and use it in GitHub Desktop.
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
# 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
@ryenski
Copy link
Author

ryenski commented Apr 11, 2017

amount_cents should default to 0 or an exception may be raised.

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