-1

It's 123.98699999999999 !

Why is that?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
RadiantHex
  • 24,907
  • 47
  • 148
  • 244
  • 7
    Unbelievable how often this question comes up. How would you represent the rational number 123987/1000? – President James K. Polk Apr 03 '10 at 22:52
  • 3
    GregS: Fraction(123987, 1000) ...? – Ken Apr 04 '10 at 01:19
  • -1: There are about a thousand duplicates for this. – nikow Apr 04 '10 at 12:41
  • 2
    @RadiantHex: Please don't remove the question body once it is answered. It makes it difficult for other readers to understand the context for the answers. – Mark Byers Apr 05 '10 at 08:22
  • possible duplicate of [Python float - str - float weirdness](http://stackoverflow.com/questions/1778368/python-float-str-float-weirdness) and probably a few hundred others. – cHao Jan 18 '12 at 20:12

3 Answers3

14

See Why can't decimal numbers be represented exactly in binary

Community
  • 1
  • 1
Plynx
  • 11,341
  • 3
  • 32
  • 33
5

The Python FAQ and tutorial address this issue pretty well, I think. More generally, both are excellent resources, well worth your time to browse if you have any interest in Python!-)

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
3

This has changed in/since Python 3.1.

See also: issue 1580


To your (already deleted) addition: Use the decimal package.

Debilski
  • 66,976
  • 12
  • 110
  • 133