In python, how can i pass float variables to a list with only two decimal places?

whmcqueary used Ask the Experts™
Say I have three dollar totals that I would like to pass to a list. I'm wanting to keep them exactly as shown in the variables below. I tried using ('%.2f' % value) but that converted it to a string in the list. I know this has to be something simple so go easy on a newbie. I'm using python 2.6 right now. Thanks.

dollar_total_1 = 2305.05
dollar_total_2 = 150.00
dollar_total_3 = 734.50
list = [dollar_total_1, dollar_total_2, dollar_total_3]
print list

Actual Result:
[2305.0500000000002, 150.0, 734.5]

Desired Result:
[2305.05, 150.00, 734.50]
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Generally, you usually want to keep the value as precise as possible.  Therefore, you usually do not want to round it.  You only want it to be rounded when displayed -- converted to the string, as you already tried.

Anyway, you can use the built-in function round(value, decplaces) (see  But it does not mean that you will get exactly what you may think to get.  When using the float type, the value will be rounded to the closest number in binary representation.  It will not be exact because for radix equal to 2 you cannot express the same values as with radix 10 having the limited number of numerals.

For money calculation, it is recommended to use another numeric type.  It is based on storing the numbers using decimal numerals representation.  Have a look at the Python standard module !decimal" (

HonorGodSoftware Engineer

Another alternative is using / passing "cents" instead of "dollars".

dollar_total_1 = 230505
dollar_total_2 = 15000
dollar_total_3 = 73450
list = [ dollar_total_1, dollar_total_2, dollar_total_3 ]

Open in new window

You should award HonorGod's comment as it is the key to the understanding of the problem.  The reason is that an abstract integer number can be technically expressed with exactly the same precision using both decimal and binary numbers.  This does not hold for fractional part of a float value.  

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial