Skip to content

Commit

Permalink
Merge pull request #557 from SpiffInc/brad/currency-formats
Browse files Browse the repository at this point in the history
Support formatting excel currencies
  • Loading branch information
simonoff authored Mar 19, 2022
2 parents 0aaf0d5 + 6d3f0e8 commit 3ae128f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added test/files/currency-euro.xlsx
Binary file not shown.
Binary file added test/files/currency-uk.xlsx
Binary file not shown.
Binary file added test/files/currency-us.xlsx
Binary file not shown.

0 comments on commit 3ae128f

Please sign in to comment.