Keep trailing zeroes in python
Question: I am writing a class to represent money, and one issue I've been running into
is that `"1.50" != str(1.50)`. str(1.50) equals 1.5, and alll of a sudden,
POOF. 45 cents have vanished and the amount is now 1 dollar and 5 cents. not
one dollar and 50 cents. Any way I could prevent str from doing this, or am I
doing something wrong? This is Python 2 BTW.


Answer: You can use the `format` method on strings to specify how many decimal places
you want to represent:

    
    
    >>> "{:.2f}".format(1.5)
    '1.50'
    

But even better would be to use the [`decimal
module`](http://docs.python.org/2/library/decimal.html) for representing
money, since representation issues with binary floats can give you slightly
off results if you're doing arithmetic. The documentation for that module
mentions some of those issues specifically - one of the most interesting ones
for money applications is:

    
    
    >>> 0.1+0.1+0.1-0.3
    5.551115123125783e-17
    >>> from decimal import Decimal
    >>> Decimal('.1') + Decimal('.1') + Decimal('.1') - Decimal('.3')
    Decimal('0.0')
    

