Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Excel show decimal values correctly although no information is stored in the worksheet.xml and styles.xml about the precision of the decimal?

Here are the steps I am following to examine the behavior:

1# Create an Excel(xlsx) file, put 1234.56789 in a cell, save it.
2# Extract the .xlsx file, see the sheet1.xml and styles.xml files.

In the xl/worksheets/Sheet1.xml you will see following:

<sheetData><row r="1" spans="1:1" x14ac:dyDescent="0.25"><c r="A1" s="1"
<v>25778.567889999998</v>
</c></row></sheetData>

And there is no information in the Styles.xml about the precision of the original number which is 1234.56789.

How does Excel show the original value when we open it in MS Excel?
How does it reconstruct that original number although there is no information in the extracted files?

Note that I did not change the cell format type or anything, so the cell format is General.

I understand that because Excel is storing the number as floating point, it cannot save the value to perfect accuracy, what I can't understand is how it retains the original value when I open that in MS Excel. I am facing an issue regarding reading Excel files using OpenXML SDK, but before asking about that issue I am looking for answer to this more fundamental question.

like image 529
Thunderstruck Avatar asked Nov 01 '22 11:11

Thunderstruck


1 Answers

It just limits input precision to 15 digits, e.g. try to put 25778.567889999955 in cell and see it is immediately converted (in Excel itself) to 25778.5678899999 and 1.23456789123456789 becomes 1.23456789123456. For more details on numeric representation internals and how it affects calculations see this wonderful wikipedia article.

like image 151
dewaffled Avatar answered Nov 15 '22 06:11

dewaffled