Skip to content

Strip dotted currency symbols that do not start the input - #204

Open
dhyabi2 wants to merge 1 commit into
RubyMoney:mainfrom
dhyabi2:fix/dotted-currency-symbol
Open

dhyabi2 wants to merge 1 commit into
RubyMoney:mainfrom
dhyabi2:fix/dotted-currency-symbol

Conversation

@dhyabi2

@dhyabi2 dhyabi2 commented Dec 3, 2026

Copy link
Copy Markdown

Monetize::Parser#parse removes the currency symbol only when it is at the very start of the input (/^#{currency.symbol}/). When anything comes before it, such as a minus sign or the ISO code, the symbol is left to the generic [^\d.,'-]+ filter. That filter keeps dots, so the dot in symbols like Rs. (NPR) or kr. (DKK, ISK) is read as the decimal mark:

Monetize.parse('-Rs.1,500', 'NPR') # => -1.50 NPR    (expected -1,500.00 NPR)
Monetize.parse('-kr.10', 'DKK')    # => -0.10 DKK    (expected -10.00 DKK)
Monetize.parse('NPR Rs.10')        # => 0.10 NPR     (expected 10.00 NPR)
Monetize.parse!('-د.م.10', 'MAD')  # => ParseError "Invalid amount (multiple delimiters)"

The same inputs without the leading - or code already parse correctly ('kr.123,45' has a spec), and "-#{symbol}5.95" is covered by specs for every entry in CURRENCY_SYMBOLS, but none of those symbols contain a dot.

Change: the symbol is now removed as a literal string wherever it appears, before the character filter runs (remove_currency_symbol). Using a string in gsub also stops the symbol from being read as a regular expression. Currencies with no symbol are left alone.

I checked every currency in Money::Currency.all with "#{symbol}10", "#{symbol} 10", "10 #{symbol}", "#{symbol}-10" and "-#{symbol}10": before the change 23 currencies (DKK, ISK, NPR, BGN, BOB, PAB, AED, SAR, MAD, OMR and others) gave a wrong value or an error for "-#{symbol}10". After the change all of them give 10 or -10.

Added a spec in spec/monetize_spec.rb (ignores the dot of a currency symbol that does not start the input) and a CHANGELOG line under Unreleased.

Tests: I did not run the project's bundle exec rspec spec. rspec and the money gem's i18n dependency were not installed where I worked, so I ran the two spec files with money 7 from source and a small stand-in for the RSpec DSL they use:

  • before the fix, with the new spec: spec/monetize_spec.rb 328 examples, 1 failure (the new spec: -kr.10 gave -0.10 DKK); spec/core_extensions_spec.rb 25 examples, 0 failures
  • after the fix: 328 examples, 0 failures; 25 examples, 0 failures

The parser only removed the currency symbol when it was the first thing
in the input (`^#{currency.symbol}`). Anywhere else the symbol fell to
the generic character filter, which keeps dots, so the dot of symbols
such as "Rs." or "kr." was read as a decimal mark:

    Monetize.parse('-Rs.1,500', 'NPR') # => -1.50 NPR, expected -1,500.00
    Monetize.parse('-kr.10', 'DKK')    # => -0.10 DKK, expected -10.00
    Monetize.parse('NPR Rs.10')        # => 0.10 NPR, expected 10.00

Remove the symbol as a literal string wherever it appears before the
character filter runs.

@sunny sunny left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix!

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

Successfully merging this pull request may close these issues.

2 participants