Python

Formatting Float Values

Be careful when formatting floats using Python's string functions. It is common to do something like this:

print "%s?geocode=%f,%f,%f" % (url, coordinates[1], coordinates[0], coordinates[2])

However, if you have are working with float values that have high precision, Python will round the values. Instead of this:

-88.4449935793094, 40.1712060862677, 0

You'll get this:

-88.444994, 40.171206, 0.000000

If you instead treat the float value as a string, you'll see the full result:

print "%s?geocode=%s,%s,%s" % (url, coordinates[1], coordinates[0], coordinates[2])