Why not FP for Money?

Dan Bishop danb_83 at yahoo.com
Fri Sep 24 13:25:13 EDT 2004


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote in message news:<7xpt4c3v3w.fsf at ruckus.brouhaha.com>...
> aleaxit at yahoo.com (Alex Martelli) writes:
> > yep, str(35.72) == '35.72'.
> 
> Hmm.
> 
>     >>> 35.72
>     35.719999999999999
> 
> I wonder why str(35.72) is different from repr(35.72).

Because str and repr have different purposes.

repr(x) is meant to provide enough information to re-create x, so all
float values must be distinguished from each other.   This requires at
least 17 significant digits, so repr(x) == '%.17g' % x.

str(x), however, is meant to return a "nice" string representation,
and so it only uses '%.12g' % x (ignoring 5 "noise" digits at the end
of repr(x)).



More information about the Python-list mailing list