elixirmoney / money

Elixir library for working with Money safer, easier, and fun... Is an interpretation of the Fowler's Money pattern in fun.prog.
https://hex.pm/packages/money/
MIT License
826 stars 139 forks source link

Allow rounding Money values with Decimal #177

Closed noahsettersten closed 2 years ago

noahsettersten commented 2 years ago

In a recent project, I found it useful to round a %Money{} value for displaying a summary of an amount without any fractional portion. To that end, I've added a basic round/1,2 function that will round the amount in a Money struct. Rounding takes place based on the currency's exponent and will use the rounding mode set in the Decimal context.

This pull request may also address the request for rounding in https://github.com/elixirmoney/money/issues/159.


Notes on the API

The places argument is somewhat awkward, but I chose to pass through the same value used by Decimal instead of manipulating it internally.

I've also removed the import from Kernel for the global round function, so that we can define our own round within Money.

Nitrino commented 2 years ago

@noahsettersten Thanks ❤️

noahsettersten commented 2 years ago

Thank you, @Nitrino!