diff --git a/lib/roo/excelx/cell/number.rb b/lib/roo/excelx/cell/number.rb index 7ea48b58..5cd9b552 100644 --- a/lib/roo/excelx/cell/number.rb +++ b/lib/roo/excelx/cell/number.rb @@ -66,6 +66,16 @@ def generate_formatter(format) when '##0.0E+0' then '%.1E' when "_-* #,##0.00\\ _€_-;\\-* #,##0.00\\ _€_-;_-* \"-\"??\\ _€_-;_-@_-" then number_format('%.2f', '-%.2f') when '@' then proc { |number| number } + when /^(?:_\()?"([^"]*)"(?:\* )?([^_]+)/ + proc do |number| + formatted_number = generate_formatter($2).call(number) + "#{$1}#{formatted_number}" + end + when /^_[- \(]\[\$([^-]*)[^#@]+([^_]+)/ + proc do |number| + formatted_number = generate_formatter($2).call(number) + "#{$1}#{formatted_number}" + end else raise "Unknown format: #{format.inspect}" end diff --git a/spec/lib/roo/excelx_spec.rb b/spec/lib/roo/excelx_spec.rb index 7cc9b13e..a1398aa2 100755 --- a/spec/lib/roo/excelx_spec.rb +++ b/spec/lib/roo/excelx_spec.rb @@ -326,6 +326,30 @@ expect(subject.formatted_value(4, 1)).to eq '05010' end end + + context 'contains US currency' do + let(:path) { 'test/files/currency-us.xlsx' } + + it 'returns a zero-padded number' do + expect(subject.formatted_value(4, 1)).to eq '$20.51' + end + end + + context 'contains euro currency' do + let(:path) { 'test/files/currency-euro.xlsx' } + + it 'returns a zero-padded number' do + expect(subject.formatted_value(4, 1)).to eq '€20.51' + end + end + + context 'contains uk currency' do + let(:path) { 'test/files/currency-uk.xlsx' } + + it 'returns a zero-padded number' do + expect(subject.formatted_value(4, 1)).to eq '£20.51' + end + end end describe '#row' do diff --git a/test/files/currency-euro.xlsx b/test/files/currency-euro.xlsx new file mode 100644 index 00000000..c160836c Binary files /dev/null and b/test/files/currency-euro.xlsx differ diff --git a/test/files/currency-uk.xlsx b/test/files/currency-uk.xlsx new file mode 100644 index 00000000..8b709dd0 Binary files /dev/null and b/test/files/currency-uk.xlsx differ diff --git a/test/files/currency-us.xlsx b/test/files/currency-us.xlsx new file mode 100644 index 00000000..a11dfa5b Binary files /dev/null and b/test/files/currency-us.xlsx differ