question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unexpected number formatting

See original GitHub issue

Title: unexpected number formatting

Hi, I found unexpected number formatting when i use xlsxwriter

Here is some code that demonstrates the problem: Also you can check github repo with docker containers https://github.com/p0m1d0rka/xlsxwriter_weird_formatting

Xlsxwriter will add additional “\” before " " and “,” in excel and break format.

What am I doing wrong?

import xlsxwriter
import os
filename = 'xlsxwriter_test_python' + os.environ['PYTHON_VERSION'] + '.xlsx'

workbook = xlsxwriter.Workbook(filename)
worksheet = workbook.add_worksheet()
big_numbers_format = workbook.add_format({'num_format': '# ##0'})
percent_format = workbook.add_format({'num_format': '0,00%'})

#expected in excel 100 000 000, got 100000 000
worksheet.write_number  (0, 0, 100000000, big_numbers_format)

#expected 99,99% in excel got 100%
worksheet.write_number  (0, 1, 0.9999, percent_format)

workbook.close()


Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Nov 20, 2019

The reason this happens is that Excel stores the format in xlsx file in the US format (with “.” decimal and “,” thousand separator). When Excel reads and displays the file it uses the values set in Windows for the locale (“,” and " " in your case).

Thanks for the detailed report by the way. That is the first time anyone has supplied a docker image, even though the sample code was enough.

1reaction
jmcnamaracommented, Nov 20, 2019

Thanks for that. It looks like your manual edits invoke the Excel builtin formats. See this section of the docs: “For backwards compatibility XlsxWriter also supports Excel’s built-in formats which are set via an index number, rather than a string” https://xlsxwriter.readthedocs.io/format.html#format-set-num-format

So the following should give you what you want (note the numbers instead of format strings:

import xlsxwriter
import os

filename = 'test.xlsx'

workbook = xlsxwriter.Workbook(filename)
worksheet = workbook.add_worksheet()

worksheet.set_column(0, 0, 20)

big_numbers_format = workbook.add_format({'num_format': 3})
percent_format = workbook.add_format({'num_format': 10})

worksheet.write_number  (0, 0, 100000000, big_numbers_format)

worksheet.write_number  (0, 1, 0.9999, percent_format)

workbook.close()

The following may also work:

big_numbers_format = workbook.add_format({'num_format': '#,##0'})
percent_format = workbook.add_format({'num_format': '0.00%'})

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Unexpected number formatting in printed String - Stack Overflow
Unexpected number formatting in printed String · This has nothing to do with printf at all, just operator precedence and octal numbers. –...
Read more >
[Solved]-unexpected number formatting output-C#
I assume you would need to build a custom numeric format string. value.ToString("#,0.##########", new CultureInfo("fr-CA")). The leading #,0 part introduces ...
Read more >
Custom Number Format Show Unexpected Number When the ...
When using a custom number format, the negative numbers are shown as positive numbers. For example, when showing the numbers in Million, the...
Read more >
number formatted is unexpected. - CodeRanch
I have a problem to make the number formatted to 2 points. This is the code i use and i expect to get...
Read more >
Solved: Unexpected output of number format action for Germ...
Solved: Hello, I'm using the format number action as follows: My desired output based on the format would be "1.200,00" but it returns...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found