-1

It's 123.98699999999999 !

Why is that?

ThinkingStiff
  • 63,812
  • 29
  • 142
  • 238
RadiantHex
  • 23,687
  • 47
  • 145
  • 240
  • 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,241
  • 3
  • 31
  • 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
  • 811,175
  • 162
  • 1,198
  • 1,373
3

This has changed in/since Python 3.1.

See also: issue 1580


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

Debilski
  • 65,290
  • 11
  • 108
  • 132