Getting fractional part from a float without using string operations
Tino Wildenhain
tino at wildenhain.de
Wed Nov 19 09:03:46 EST 2008
srinivasan srinivas wrote:
> Yes. But it didn't give only the expected decimals.
> For ex:
> >>> a = 1.23
> >>> abs(int(a) -a)
> 0.22999999999999998
>
> I would like to get the result '0.23' only.
well, thats what get stored internally - there
is no way around it if you are using floating
point numbers:
>>> 0.23
0.23000000000000001
but str() handles the rounding correctly:
>>> print 0.23
0.23
>>> print abs(int(a) -a)
0.23
See also http://en.wikipedia.org/wiki/Floating_point
for the problems with FP figures.
Regards
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20081119/2ba89f13/attachment-0001.bin>
More information about the Python-list
mailing list