[GIS] How to get a string from a Double with a fixed number of decimals

arcgis-10.0field-calculator

I have a featureclass (in a File Geodatabase), with a particular field of type Double. I want to copy it over to a text field, so that only 3 decimal places are shown.

For example: 34.78902345 becomes 34.789

I have gone through this question: How to convert a double field to a string?, but that does not solve my issue. I have even tried to format the double field, but even then the text field contains all the decimals.

How do I do this in ArcGIS 10.0?

Best Answer

You should be able to use string formatting:

'{0:.3f}'.format(your_floating_point_value)

Example:

>>> '{0:.3f}'.format(34.78902345)
'34.789'

Here is the Python string formatting cookbook for reference.

To use this in the field calculator you can create a function:

def my_format(val):
  return '{0:.3f}'.format(val)