Excel - Excel time format / accuracy

Asked By Pete Fraser on 21-Nov-07 09:47 PM
A two part question.
I'm reading in a csv file to Excel 2007.
It has a series of time values, for example:

I can't work out how to format the cell as a
time, and keep the decimal (the display rounds to the
nearest second).

I decided just to ignore that, and just formatted as a number.
It seems to display correctly (as the correct portion of a
day), but only to an accuracy of the first three input digits
after the decimal point.

For example, the time above would convert to
0.7121043171296300 which is equivalent to

What happened to my accuracy, and how do I
get it back?



JM replied on 21-Nov-07 11:30 PM
I think your problem is that XL can only accomodate numbers to 15 digits.
Anything after that is truncated.
Pete Fraser replied on 22-Nov-07 02:03 AM
I am not getting that though.
it is only giving me 9 digits
(2 hours, 2 minutes, 2 seconds, 3 miilliseconds).
JM replied on 22-Nov-07 02:39 AM
As you have already noted, the underlying decimal value is:

I count more than 9 digits.
Pete Fraser replied on 22-Nov-07 12:48 PM
Got it now, thanks.
Excel is rounding the input to nine digits then
doing the conversion.

Is there any way to coerce Excel into doing
arithmetic more accurately, or do I need to
fall back on Python and Mathematica?

Also, it's a minor detail, but do you know how
to get Excel's time format to display to a resolution
better than one second.


David Biddulph replied on 22-Nov-07 01:41 PM
Format as hh:mm:ss.000
David Biddulph

JM replied on 22-Nov-07 02:12 PM
XL chops off the number after 15 digits and puts in zeros (placeholders).
You can get XL to display all of the digits by formatting the cells as text,
but that won't help accuracy if you perform arithmetic operations.

You may need to use something other than XL.  You might also look around for
any XL add-ins that might help overcome this limitation (I think I've seen
one before, but have no clue where to find it now).

You could look at using VBA since it has data types that can accomodate more
digits.  Your data would need to be stored in the cell as text.  Suppose A1
is '1.2345678901234567890, then Test(A1, "*", 2) returns 2.469135780246913578

I don't know of a way to get XL to display times to a greater degree of
accuracy than hh:mm:ss.00

Function Test(strInput1 As String, _
strOperation As String, strInput2 As String) As String
Dim varInput1 As Variant
Dim varInput2 As Variant

varInput1 = CDec(strInput1)
varInput2 = CDec(strInput2)

Select Case strOperation
Case "*"
Test = CStr(varInput1 * varInput2)
Case "/"
Test = CStr(varInput1 / varInput2)
Case "+"
Test = CStr(varInput1 + varInput2)
Case "-"
Test = CStr(varInput1 - varInput2)
End Select

End Function
JM replied on 22-Nov-07 02:23 PM
Forgot to mention - you might try to search this site for any old posts
w/similar issues and maybe a better solution to your problem.

Specifically, Jerry W. Lewis is one person who is quite knowledgable about
XL accuracy issues.
post_a_repl replied on 22-Nov-07 05:44 PM
Thanks for the plug.

Excel time values are stored as a decimal fraction of 24 hours.  Thus the time
5:05:25.8127339 PM
would be represented as
which equals 0.712104314049769, not the imported value of 0.7121043171296300
which corresponds to the rounded time of 5:05:25.813 PM.

Only 8 decimal places are required to specify a time to a thousandth of a
second, so Excel's 15 digit limit is irrelevant here.  You can display the
time to a thousandth of a second with the custom format "h:mm:ss.000 AM/PM".

Why Excel will not import time values to greater accuracy than a thousandth
of a second is a mystery.  Excel dates are stored as the number of days since
1900, and date-time values as the date value plus the time value.  That only
requires 13 figures until the year 2173, so Excel could easily support an
additional figure or two in time values without losing accuracy in date/time
values.  I am not aware of why MS chose not to do so.

JM replied on 22-Nov-07 11:29 PM
Thanks for taking the time to respond Jerry.  Hope you don't mind my
name-dropping, but I know I've learned a few things from your posts dealing
w/XL accuracy issues so it's probably where I would start.

I was a little perplexed the number could not be displayed to more places
than hh:mm:ss.000 (I see I left a 0 off my earlier post) - but was unsure if
there was a reason for it or just MS's decision.  Your explanation regarding
that is much appreciated.