I am developing a Python script that uses xlrd to obtain data from xls files. I cannot figure out, however, how to print floating point numbers with the same precision as they have in the Excel file.
I tried using repr as suggested in https://stackoverflow.com/a/3481575/1296490 but this still gives different precision from the one I want.
For example, the Excel file has a cell with value -1.62717010683527 , then using
str(worksheet.cell(i,j).value) returns -1.62717010684,
while repr( worksheet.cell(i,j).value) returns -1.6271701068352695.
Using str(Decimal(worksheet.cell(i,j).value)) produces -1.6271701068352695095187044671547482721507549285888671875
None of the above gives me the original value from Excel. I have to process many such numbers each with different number of digits after the dot and it is not suitable to use %.10f etc.
Precision in Excel is confined to 15 significant figures.
Use
str( "%0.15g" % cell.value )to achieve what you want:g
- Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise.(http://docs.python.org/release/2.4.4/lib/typesseq-strings.html)
Do not forget
0.in"%0.15".