Shopify / money

Manage money in Shopify with a class that won't lose pennies during division
http://shopify.github.io/money
MIT License
286 stars 38 forks source link

Allow rounding bypass when creating Money objects #306

Open dmasciotra opened 2 weeks ago

dmasciotra commented 2 weeks ago

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 commented 2 weeks ago

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