Floating numbers

Philip Semanchuk philip at semanchuk.com
Thu Aug 12 23:25:07 CEST 2010


On Aug 12, 2010, at 4:43 PM, Bradley Hintze wrote:

> Hi all.
>
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

Hi Bradley,
Use the Decimal type instead. It's not as convenient as float, but it  
will give you a consistent representation of your numbers.

The behavior of floating point numbers surprises a lot of people and  
has been discussed at length a number of times here (and elsewhere).

If you're in the mood for EXTREMELY thorough coverage of the subject,  
you can read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

There's a gentler discussion of it here:
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

HTH
Philip



More information about the Python-list mailing list