Skip to content

Commit a068081

Browse files
authored
Merge pull request #147 from trushkevich/deduce-decimal-mark-when-major-zero
Make parser deduce decimal mark when there is only one delimiter in a string and major is 0
2 parents 874c6f6 + 3212d38 commit a068081

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.11.0
4+
- When parsing a string assume a single separator to be a decimal mark when number starts with 0
5+
36
## 1.10.0
47
- When using the `assume_from_symbol` option, the currency in the input string will be used over the assumed currency based on symbol. For example, `$1.05 CAD` will use `CAD` instead of `USD`.
58

lib/monetize/parser.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ def extract_major_minor_with_tentative_delimiter(num, delimiter)
126126
else
127127
possible_major, possible_minor = split_major_minor(num, delimiter)
128128

129-
if possible_minor.length != 3 || possible_major.length > 3 || delimiter == '.'
130-
# Doesn't look like thousands separator
129+
# Doesn't look like thousands separator
130+
is_decimal_mark = possible_minor.length != 3 ||
131+
possible_major.length > 3 ||
132+
possible_major.to_i == 0 ||
133+
delimiter == '.'
134+
135+
if is_decimal_mark
131136
[possible_major, possible_minor]
132137
else
133138
["#{possible_major}#{possible_minor}", '00']

lib/monetize/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# encoding: utf-8
22

33
module Monetize
4-
VERSION = '1.10.0'
4+
VERSION = '1.11.0'
55
end

spec/monetize_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,29 @@
320320
expect('€8.883.331,0034'.to_money('EU4')).to eq Money.new(8_883_331_0034, 'EU4')
321321
# rubocop:enable Style/NumericLiterals
322322
end
323+
324+
it 'parses strings deducing a decimal mark when a single delimiter is used and major is 0' do
325+
expect('$0,4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
326+
expect('€0.4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
327+
328+
expect('$0,04'.to_money('BAR')).to eq Money.new(400, 'BAR')
329+
expect('€0.04'.to_money('EU4')).to eq Money.new(400, 'EU4')
330+
331+
expect('$0,004'.to_money('BAR')).to eq Money.new(40, 'BAR')
332+
expect('€0.004'.to_money('EU4')).to eq Money.new(40, 'EU4')
333+
334+
expect('$0,0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
335+
expect('€0.0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
336+
337+
expect('$0,0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
338+
expect('€0.0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
339+
340+
expect('$0,0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
341+
expect('€0.0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
342+
343+
expect('$0,5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
344+
expect('€0.5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
345+
end
323346
end
324347
end
325348

0 commit comments

Comments
 (0)