How to detect a double's significant digits
Steven Bethard
steven.bethard at gmail.com
Thu May 5 14:30:53 EDT 2005
mrstephengross wrote:
>>But, assuming you have your numbers as strings, I would suggest
>
> looking
> at str.split() and len().
>
> Well, the numbers are in fact stored as numbers, so string processing
> won't work.
How about:
py> def digits(f):
... return len(str(f).split('.')[1].rstrip('0'))
...
py> for f in [0.103, 0.1030, 0.0103, 0.010300]:
... print f, digits(f)
...
0.103 3
0.103 3
0.0103 4
0.0103 4
I believe the rstrip is unnecessary because I think str() will never
produce additional following zeros, but you can guarantee it by calling
rstrip if you want.
Note that, for example, 0.103 and 0.1030 are identical as far as Python
is concerned, so I hope you're not hoping to show a difference between
these two...
STeVe
More information about the Python-list
mailing list