mirror of
https://github.com/jmcnamara/libxlsxwriter.git
synced 2026-05-15 14:15:54 -06:00
[GH-ISSUE #62] Question about writing a number #53
Labels
No labels
awaiting user feedback
bug
cmake
cmake
docs
feature request
in progress
long term
medium term
medium term
pull-request
question
question
ready to close
short term
under investigation
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/libxlsxwriter#53
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @AlfonsoUceda on GitHub (Jul 6, 2016).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/62
Originally assigned to: @jmcnamara on GitHub.
Hi @jmcnamara
I need to ask you something related writing a number in an Excel, because I'm porting my old XLSX writer to the new writer based in this library.
The value I write in the excel is the following
0.041666666666666664with worksheet_write_number function, the same for my both renderers and the format[h]:mm:sswith format_set_num_format function.I've noticed the old renderer write the following XML for the cell:
and the new renderer writes
I've seen the attribute is optional because
nis number and it's default but you can see the values are different, If I translate those values in ruby console I getthe second one gives me a wrong value.
FYI, the old renderer is written in ruby, the new renderer is a wrapper in ruby of your lib.
Testing with a C example:
the cell is like this (truncates the last four):
@jmcnamara commented on GitHub (Jul 7, 2016):
Hi Alfonso,
Excel stores number as IEEE754 doubles. These have 15 significant digits of precision. So the best precision you could get with this number is:
And not as you expected:
You can verify this yourself by pasting 0.041666666666666664 into a cell in Excel and looking at the XML output. With Excel 2013 I get the following:
Which is:
Libxlsxwriter uses the same precision and IEEE754 doubles as Excel.
You "old" file producer may use a higher precision in the output format, or maybe Ruby has higher precision floating point but either way if you open the file in Excel and resave it you will probably lose the precision.
John
@AlfonsoUceda commented on GitHub (Jul 7, 2016):
Hi @jmcnamara
I saved the excel generated by the old renderer and Excel transforms the value from
0.041666666666666664to4.1666666666666664E-2, and the number in the new renderer is4.1666666666666657E-2.4.1666666666666664E-2#=> 0.0416666666666666644.1666666666666657E-2#=> 0.04166666666666666☝️ you can see the length is the same.
@jmcnamara commented on GitHub (Jul 7, 2016):
What happens if you paste the number manually into Excel, save the file and view the xml?
@AlfonsoUceda commented on GitHub (Jul 7, 2016):
It's used as shared string :S even If I put the format as a number in the excel, I use excel 2016 but I'll ask a mate to do it with a lower version of excel.
@AlfonsoUceda commented on GitHub (Jul 7, 2016):
@jmcnamara one thing I saw is, If I write in a cell
01:00:00is transformed to1:00:00, then I save the excel and unzipped it, when I see the cell XML I have the following:EDIT:
@jmcnamara A mate with a v14 of excel, he saved the value as number and normal, and they are shared strings in the XML
@AlfonsoUceda commented on GitHub (Jul 7, 2016):
@jmcnamara With "02:00:00" is right because the float precision is 17 digist.
@jmcnamara commented on GitHub (Jul 7, 2016):
Interesting.
Times in Excel are expressed as a percentage of the day so
1:00:00is 1/24 which is:Excel uses IEEE754 doubles internally for calculations. This has a limited precision of 15 digits (it can be slightly higher for some numbers).
So when Excel writes the number to a file it uses some formatting to convert from the internal double to the external string representation. From my analysis this format is
"%.16g". At least that gives the most consistent results when comparing against files generated by Excel and there are a lot of tests in the Python/Perl/C versions of the library that do that.However in the case above the format looks like
"%.17g":Gives:
However, if I change the test suite to
"%.17g"then some of the test cases fail so that isn't a general solution.Ultimately this is just an artifact of handling fixed precision floating point numbers.
Beyond 15/16 digits the C and Ruby versions are both wrong, just differently.
@jmcnamara commented on GitHub (Jul 7, 2016):
BTW, you can see the same thing in Ruby:
@AlfonsoUceda commented on GitHub (Jul 8, 2016):
@jmcnamara thank you for answer and investigation, we are going to use the text format because it's work in our integration.
@jmcnamara commented on GitHub (Jul 8, 2016):
Hi Alfonso.
Thanks for the update.
John