Shopify/money

Rounding up (or down) on partial cents?

wwahammy opened this issue · 1 comments

First off, I really like this Gem, it looks exactly like what we need. Thanks for all the work.

I'm trying to understand the best way to use Money for calculating amounts of money involving partial cents. For example, for a certain fee calculation, I need to always round up calculations if there is a fraction of cents. For example, if the fee is 2.2% on $1, I need to the result to always be $0.03. When I run Money.new(1, 'usd') * 0.022 though, I get the result of $0.02. I assume the gem has to handle this use-case so what is the correct way to handle this?

Hi 👋 it sounds like you'd want the equivalent of:

currency = Money::Currency.find('usd')
amount = (BigDecimal(1) * 0.022).round(currency.minor_units, :up)
Money.new(amount, currency)
=> #<Money value:0.03 currency:USD>

We could expose the rounding option as an argument for the Money initializer and pass that along when we create a BigDecimal (see all modes here), so you'd get something like:

Money.new(BigDecimal(1) * 0.022, 'usd', round: :up) 

WDYT?