monetize icon indicating copy to clipboard operation
monetize copied to clipboard

Deduce decimal mark when parsing a string with a number starting with 0

Open trushkevich opened this issue 4 years ago • 1 comments

Hi, I've just found a possibility to improve a bit parsing a string via #to_money (Monetize.parse)

here, take a look

'0.1'.to_money('BTC')
# => #<Money fractional:10000000.0 currency:BTC>
'0,1'.to_money('BTC')
# => #<Money fractional:10000000.0 currency:BTC>
'0.01'.to_money('BTC')
# => #<Money fractional:1000000.0 currency:BTC>
'0,01'.to_money('BTC')
# => #<Money fractional:1000000.0 currency:BTC>
'0.001'.to_money('BTC')
# => #<Money fractional:100000.0 currency:BTC>
'0,001'.to_money('BTC')
# => #<Money fractional:100000000.0 currency:BTC>    <------ THIS
'0.0001'.to_money('BTC')
# => #<Money fractional:10000.0 currency:BTC>
'0,0001'.to_money('BTC')
# => #<Money fractional:10000.0 currency:BTC>

I understand that it is impossible to make sure the behavior is 100% consistent across all possible cases as thousands separator still has to be supported and e.g. 1,500.to_money('LTC') is much more likely 1500 LTC than 1.5 LTC. But I guess it is safe to assume that if a number starts with a zero like in the examples above then the delimiter is a decimal mark and not a thousands separator.

I created a PR https://github.com/RubyMoney/monetize/pull/147, what do you think ?

trushkevich avatar Feb 08 '21 15:02 trushkevich

Brilliant, thank you for fixing this 👍

antstorm avatar Feb 10 '21 22:02 antstorm