> Actually, the numbers have a lot of *bits* after the *binary* point.
> What are you using to display them as decimal numbers? AFAIK Excel
> will not display more than 15 decimal digits of precision.
>
> Example:
> Type 318.939953634425 into cell A1
> Type =A1+0.0000000000001 into cell A2
> Copy the formula into cells A3:A20
> Format cells A1:A20 to show 14 decimal places
>
> You will see
> 5 x 318.93995363442500
> 8 x 318.93995363442600
> 7 x 318.93995363442700
> but there are actually 20 *different* values, which xlrd will dig out
> for you, to the full precision that Excel uses to store them, not the
> limited precision that Excel uses to display them.
>
> > Now when xlrd reads such number it
> > becomes 318.93995363442485. (I am doing the reading according to
> > simple example in readme).
>
> It doesn't "become" anything. Excel stores numbers in 64-bit IEEE 754
> floating point format. xlrd reproduces them without change.
>
> If you wish to round the results to Excel's notion of precision, you
> can use the round() builtin function, or something like "%.15g" %
> number
>
Aaaaah! Thank you very much for the explanation. I did not know that
Excel shows only 15 digits of precision. I increased the number of
digits shown in Excel until appearance of zeroes and assumed that if
zeroes appear that is the end of the precision. As you pointed out that
is not true. I made a quick experiment created formula whose result is
1/3 and behold zeroes appear when there should be none. I checked
OpenoOffice.org 2.3 and Excel 2007, both give zeroes after 15 digits.
But if the actual value is stored in bigger precision, then this is a
bit misleading. Although not unexpected from Microsoft :)
Thank you very much for the answer, that was precisely what I needed.
And now I know that when you save the file under csv in Excel or
OpenOffice.org you lose precision, not that this is very important in
applications I use, but still very valuable information. Thanks again!
Vaidotas Zemlys
I understand that the full precision of a number is available in xlrd. My question is whether or not there is a way to discover, via info available in xlrd, the precision with which a number was entered in the Excel file.
My scenario is that a user enters numbers in Excel .. usually in a General format cell .. sometimes adjusting the precision displayed manually, but not always. I need to read the files, do some manipulation, and output the data as text. I need the number values in the output text to appear as they were displayed in the cell by Excel.
I have a some data for few numbers I entered, read and output below. It appears that Excel defaults to a 9 digit precision,
but that can be changed by manually adjusting the precision via the toolbar (see Excel Cell Display for last number). For the default entry / display, xlrd get a 'General' format ..no information about the precision on entry.
For the manually adjusted one .. it seems that the format_str reflects the precision displayed in the cell.
From the difference between the "%-25s" and "%-25.20f" data output it appears that Python "%s" formatting default maxes out at 15 digits .. or is something else going on?
Excel Version Excel 97-2003xlrd version xlrd-0.7.1-py2.7-win32.eggEntered value Excel Cell Display "%-25s" % cell.value "%-25.20f" % cell.value xlrd cell format_str0.0000625 0.0000625 6.25e-05 0.00006250000000000000 General0.0009765625 0.000976563 0.0009765625 0.00097656250000000000 General0.0006866455078125 0.000686646 0.000686645507812 0.00068664550781250000 General0.0006866455078125 0.0006866455078125000 0.000686645507812 0.00068664550781250000 0.0000000000000000000
You're already losing precision then. Excel uses IEEE 754 floating
point for all its numeric data, which is what xlrd is reading:
<http://support.microsoft.com/kb/78113>
Unfortunately, I do not have any quick, easy suggestions to get the
precision you are after. (Python itself does support
arbitrary-precision decimals, but I don't know of a ready-made
Excel-like GUI that does.)
John Y.
On Mon, Apr 25, 2011 at 12:14 AM, Kelly <morrel...@gmail.com> wrote:
> But, the number of
> fractional digits in the data can range from 0 up to 20 .. at least in data
> we have been working with so far.
You're already losing precision then. Excel uses IEEE 754 floating
point for all its numeric data, which is what xlrd is reading:
It's worse. That article says that IEEE 754 restricts them to 15 decimal digits of precision. This is an imprecise statement. The IEEE 64-bit binary format provides for a mantissa of 53 significant bits, equivalent to 15.95 decimal digits of precision. What they actually do with it is to muck about trying to pretend that it is actually an exactly-15-decimal-digits system, a bit like Python's decimal.Decimal with a precision of 15 instead of 28. See section 2 of this: http://www.cs.berkeley.edu/~wkahan/Mindless.pdf
Maybe a combination of Text cells and General cells with manually adjusted precision will be a good compromise .. will check that out. Otherwise, the only other thoughts I had were to have the user define the precision they want for all entered values in an adjacent cell .. but that seems onerous, error-prone, and likely to be forgotten on updates.
Any further thoughts you have would be appreciated .. especially if I have missed the obvious for what seems like it would be a fairly standard use case.
At the risk of venturing even a bit further off the topic of
"python-excel", I would like to mention Pyjamas <http://pyjs.org/> for
those folks who are good with Python but not so good with JavaScript,
but still want to deploy a GUI application for the Web.
John Y.