Issue 2653: string to float to int (original) (raw)

Issue2653

Created on 2008-04-18 07:17 by wr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg65602 - (view) Author: wr (wr) Date: 2008-04-18 07:17
IDLE 1.2.1 >>> print int(float('1.005')*1000) 1004
msg65604 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-04-18 07:34
Yes, and >>> 1.005 * 1000 1004.9999999999999 Please read http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate To perform exact base-10 calculation, you may use the Decimal module. Or use round() instead of the int() truncation.
msg65605 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-04-18 08:12
>>> int('%.0f' % (float('1.005')*1000)) 1005 >>> int(Decimal('1.005')*1000) 1005
History
Date User Action Args
2022-04-11 14:56:33 admin set github: 46905
2008-04-18 08:12:26 rhettinger set nosy: + rhettingermessages: +
2008-04-18 07:34:57 amaury.forgeotdarc set status: open -> closedresolution: not a bugmessages: + nosy: + amaury.forgeotdarc
2008-04-18 07:17:05 wr create