RubyMoney / monetize

A library for converting various objects into `Money` objects.
MIT License
430 stars 107 forks source link

Improves parsing where fractional subunits are not expected #159

Closed AlexMuir closed 1 year ago

AlexMuir commented 2 years ago

Parsing real world human input there's a common case where a single thousands separator is used.

For example at the moment, the following string parsings are incorrect (unless we're expecting fractional subunits)

Monetize.parse('USD 10.000')  == Money(10_00, 'USD')  # => $10.00
Monetize.parse('EUR 10,050')  == Money(10_05, 'EUR')  # => €10.05

Sometimes we expect fractional subunits (stock prices etc) but often we actually don't. A shopping comparison site will almost never expect fractional subunits.

I have therefore added the Monetize.expect_whole_subunits option. This will corrects the above scenario.

Monetize.expect_whole_subunits = true
Monetize.parse('USD 10.000')  == Money(10_000_00, 'USD')  # => $10 000.00
Monetize.parse('EUR 10,050')  == Money(10_050_00, 'EUR')  # => €10 050.05

All specs pass.