6

I'm wanting to create a field with a maximum of 1 decimal place and then calculate a value into it: (I know that all my values are < 10)

arcpy.AddField_management('C:/shapefile.shp', 'myField', "FLOAT", 2, 1, "", "", "", "NON_REQUIRED", "")
arcpy.CalculateField_management('C:/shapefile.shp', 'myField', "[MEAN]", "VB", "")

It seems that no matter what I try I can't get the number of decimal places I want - I get the full value from the field I'm calculating from : [0.888889] and when I view the field properties I just see 0 for precision and 0 for scale. The data context is ESRI shapefile. And altering the numeric settings in the field properties is not an option.

enter image description here

Anyone have any ideas?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 2, 2012 at 17:59
4
  • 1
    What is your data source? Precision and scale only apply to RDBMSes that support them, such as Oracle and SQL Server. File and personal geodatabases do not. Commented Jul 2, 2012 at 18:15
  • As I said it's from shape to shape. By shape I mean shapefile. Commented Jul 2, 2012 at 18:17
  • Oh I missed that, sorry. This looks like another case where the ESRI documentation falls short as there is no mention of shapefile support for precision/scale, although it does appear to be supported if you set them when adding a field through the UI. Commented Jul 2, 2012 at 18:30
  • Yeah - When adding a new field via both the Attribute Table and ArcToolbox - the scale and precision values are respected. Commented Jul 2, 2012 at 18:41

2 Answers 2

7

What is your data source? Precision and scale only apply to DBMSes that support them, such as Oracle and SQL Server. According to the Add Field (Data Management) documentation file and personal geodatabases do not:

  • The precision and scale of a field describe the maximum size and precision of data that can be stored in the field. The precision describes the number of digits that can be stored in the field and the scale describes the number of decimal places for float and double fields. For example, if the field value is 54.234, then scale = 3 and precision = 5. Use the following guidelines for choosing the correct field type for a given precision and scale:
    • When you create a float, double, or integer field and specify 0 for precision and scale, the tool will attempt to create a binary type field if the underlying database supports it. Personal and file geodatabases support only binary type fields, and precision and scale are ignored.
    • When you create float and double fields and specify a precision and scale, if your precision is greater than 6, use a double; otherwise, use a float. If you create a double field and specify a precision of 6 or less, a float field is created. If you create a float field and specify a precision greater than 6, a double field is created.
    • If you specify a scale of 0 and a precision of 10 or less, you should be creating integer fields. When creating integer fields, your precision should be 10 or less, or your field may be created as double.

Shapefiles, which use dBASE tables as the underlying storage format for the attribute table, DO appear to support precision and scale. I was able to add a field of type Float with Precision 6 and scale 4 and it shows up correctly in the UI. I did it both through the UI and through arcpy (AddField):

arcpy.AddField_management(fc, "test3", "FLOAT", 6, 4)

Shapefile field properties with precision 6 and scale 4 visible

answered Jul 2, 2012 at 18:20
5
  • So is it safe to assume that precision and scale also DO NOT apply to shapefiles? Commented Jul 2, 2012 at 18:22
  • 2
    Looks like shapefiles (which use dBASE tables as the underlying attribute table) DO support precision and scale although this is not spelled out in the documentation. Commented Jul 2, 2012 at 18:32
  • 1
    Wow, there's a shocker, shapefiles support it, but file geodatabase doesn't. Commented Jul 2, 2012 at 19:14
  • 2
    @Chad That's not a shock at all: precision and scale are needed precisely because shapefiles do not store floats or doubles: they store decimal-encoded ASCII strings. As such, internally they contain information about the total number of characters the field can hold. If ArcGIS is equating this with precision, then a (2,1) field cannot exist: two characters suffice for the "." and the following digit, but do not leave room for the initial digit. Specifying (3,1) ought to work, much as Blah238 has found. Commented Jul 2, 2012 at 19:43
  • 1
    I think I didn't phrase my question well. I know that shapefiles support prec. and scale. and I know how to specify them when adding a field in ArcMap. What I don't know is why the values that I pass in via scripting (AddField) are not respected. Commented Jul 3, 2012 at 16:12
1

I think that arcpy is looking for number as opposed to a string. If you look at the documentation:

field_precision (Optional) Describes the number of digits that can be stored in the field. All digits are counted no matter what side of the decimal they are on.

If the input table is a personal or file geodatabase the field precision value will be ignored.

(Data Type) Long

To check you could always open up model bulider, set up a similar gp workflow and export as a python script.

answered Jul 2, 2012 at 18:06
1
  • Good catch, but that doesn't fix it. I've updated the question. thanks Commented Jul 2, 2012 at 18:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.