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

Allow rounding bypass when creating Money objects #306

Open
dmasciotra opened this issue Jun 20, 2024 · 2 comments
Open

Allow rounding bypass when creating Money objects #306

dmasciotra opened this issue Jun 20, 2024 · 2 comments
Labels

Comments

@dmasciotra
Copy link

Description

There are some use cases where we would like to have an object contain both the currency and an amount, but not have the library auto round for us based on currency settings.
An example could be to represent the value of something to a more precise decimal than the currency supports, for purposes of bulk purchases.
A more concrete example could be to set the price of something as $0.057 USD so that when we have 100 of the item, the total cost would be $5.70 USD.

It should be noted arithmetic and logical operations should use this raw value as well.

@elfassy
Copy link
Contributor

elfassy commented Jun 20, 2024

proposal: Allow for an option to pass the number of precision to be used

Money.decimal_precision = 8

# or as a block

Money.decimal_precision(8) do
  money = Money.new(0.12345678, "USD")
  money.to_s #=> "0.12345678"
  money.format #=> "0.12"
end

by default the decimal precision is set by the currency

@elfassy elfassy added the v3 label Aug 12, 2024
@bdewater
Copy link
Contributor

How about a decimal_precision keyword argument that is nil by default, which then uses the currency precision. Seems easier to work with than global state, based on the assumption the rounding is often desired and only occasionally not (typically during a series of calculations).

When doing arithmetic, use the highest precision:

Money.new(1, "USD") + Money.new(0.12345678, "USD", decimal_precision: 8)
# 1.12345678 USD

The existing round instance method could be changed to default to the currency precision for rounding, instead of the current 0, providing an easy way to get back to a rounded amount before storing in the database or serializing for APIs.

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

No branches or pull requests

4 participants
@elfassy @bdewater @dmasciotra and others