Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding up (or down) on partial cents? #229

Open
wwahammy opened this issue Mar 4, 2021 · 1 comment
Open

Rounding up (or down) on partial cents? #229

wwahammy opened this issue Mar 4, 2021 · 1 comment

Comments

@wwahammy
Copy link

wwahammy commented Mar 4, 2021

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?

@bdewater
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants