RubyMoney/money

How to limit number of decimals with formatter?

stebo opened this issue · 3 comments

Given any currency where subunit_to_unit is > 100, how can I limit the number of shown decimals via .format?

Example:

config.register_currency = {
    iso_code:            "ABC",
    name:                "ABC",
    symbol:              "ABC",
    subunit:             "cents",
    subunit_to_unit:     10000,
    thousands_separator: ".",
    decimal_mark:        ","
  }

Now Money.from_cents(12345, "ABC").format gives "1.2345 ABC"

Any way how I could just get "1.23 ABC"?

because we are dealing with subunits and not a decimal, the formatter does not support this. Instead, you would have to consider converting from a Money object to something like a BigDecimal so you can truncate the format. I believe you are experiencing this because you are really working with fractional amounts versus subunits. It is a nuanced challenge. You can look at JPY and see how the function as they do not use subunits.

I think this is relevant for everyone working with subunits, that have a large subunit_to_unit ratio.
BTC already has its subunit Satoshis = 100000000, ETH is even more with its subunit WEI = 1000000000000000000.

Given that, having a look at BTC

Money.new(123456789, :btc).format
=> "₿1.23456789"

Would it not be interesting for the formatter to allow, shortening that by some decimals, to e.g. ₿1.2346 ? Does not mean that the actual subunit is lost, it is only about the format...

If the community finds value in such a pull request, we would accept it. I have just never experienced a traditional currency that would write $2.99 as $2.9 or even $2.90 as $2.9