Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string to float to int #46905

Closed
wr mannequin opened this issue Apr 18, 2008 · 3 comments
Closed

string to float to int #46905

wr mannequin opened this issue Apr 18, 2008 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@wr
Copy link
Mannequin

wr mannequin commented Apr 18, 2008

BPO 2653
Nosy @rhettinger, @amauryfa

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-04-18.07:34:57.608>
created_at = <Date 2008-04-18.07:17:05.251>
labels = ['type-bug', 'invalid']
title = 'string to float to int'
updated_at = <Date 2008-04-18.08:12:26.816>
user = 'https://bugs.python.org/wr'

bugs.python.org fields:

activity = <Date 2008-04-18.08:12:26.816>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2008-04-18.07:34:57.608>
closer = 'amaury.forgeotdarc'
components = ['None']
creation = <Date 2008-04-18.07:17:05.251>
creator = 'wr'
dependencies = []
files = []
hgrepos = []
issue_num = 2653
keywords = []
message_count = 3.0
messages = ['65602', '65604', '65605']
nosy_count = 3.0
nosy_names = ['rhettinger', 'amaury.forgeotdarc', 'wr']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2653'
versions = ['Python 2.5']

@wr
Copy link
Mannequin Author

wr mannequin commented Apr 18, 2008

IDLE 1.2.1      
>>> print int(float('1.005')*1000)
1004

@wr wr mannequin added the type-bug An unexpected behavior, bug, or error label Apr 18, 2008
@amauryfa
Copy link
Member

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.

@rhettinger
Copy link
Contributor

>>> int('%.0f' % (float('1.005')*1000))
1005

>>> int(Decimal('1.005')*1000)
1005

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants