Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formats #525

Merged
merged 6 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/roo/excelx/cell/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def generate_formatter(format)
when /^(0+)$/ then "%0#{$1.size}d"
when /^0\.(0+)$/ then "%.#{$1.size}f"
when '#,##0' then number_format('%.0f')
when '#,##0.00' then number_format('%.2f')
when /^#,##0.(0+)$/ then number_format("%.#{$1.size}f")
when '0%'
proc do |number|
Kernel.format('%d%%', number.to_f * 100)
Expand All @@ -64,6 +64,7 @@ def generate_formatter(format)
when '#,##0.00;[Red](#,##0.00)' then number_format('%.2f', '[Red](%.2f)')
# FIXME: not quite sure what the format should look like in this case.
when '##0.0E+0' then '%.1E'
when "_-* #,##0.00\\ _€_-;\\-* #,##0.00\\ _€_-;_-* \"-\"??\\ _€_-;_-@_-" then number_format('%.2f', '-%.2f')
when '@' then proc { |number| number }
else
raise "Unknown format: #{format.inspect}"
Expand Down
2 changes: 2 additions & 0 deletions test/excelx/cell/test_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_formats
['0.000000000', '1042.000000000'],
['#,##0', '1,042'],
['#,##0.00', '1,042.00'],
['#,##0.000', '1,042.000'],
['0%', '104200%'],
['0.00%', '104200.00%'],
['0.00E+00', '1.04E+03'],
Expand All @@ -74,6 +75,7 @@ def test_formats
['#,##0.00;(#,##0.00)', '1,042.00'],
['#,##0.00;[Red](#,##0.00)', '1,042.00'],
['##0.0E+0', '1.0E+03'],
["_-* #,##0.00\\ _€_-;\\-* #,##0.00\\ _€_-;_-* \"-\"??\\ _€_-;_-@_-", '1,042.00'],
['@', '1042']
].each do |style_format, result|
cell = Roo::Excelx::Cell::Number.new '1042', nil, [style_format], nil, nil, nil
Expand Down