English

How To: Convert a string field to a number field

Summary

In many scenarios it is necessary to convert a field of string values in an attribute table to numeric values, typically because the original data type was not set or recognized, so that proper data analysis can be performed. To preserve data integrity (especially if sharing or importing data from outside sources) the method for converting fields to different types is to copy the data to a new field of the desired type.

Three solutions are described below; one pertaining to ArcGIS for Desktop 10.1 through 10.4.1; the second solution is for ArcGIS Pro 1.x; the third is for older software (such as ArcView GIS 3.x.)

Procedure

ArcGIS Desktop 10.1 thru 10.4.1

  1. Open ArcMap, and open the layer or file geodatabase attribute table.
  2. In ArcMap, the data type of the field can be checked by right-clicking the Field header and selecting Properties. In this example, a String type field is converted to Double.
    A picture of the Field Properties window.
  3. On the top-left of the Table window, click the Table Options drop-down button User-added image and select Add Field.
    1. Create a field of type Double.
    2. Provide a Name.
    3. Set the Precision and Scale if desired..
    4. Click OK.
  4. Right-click the newly created field's header and select Field Calculator.
  5. If the string field contains only numeric values, the following provides the desired result:
    1. Enter [String field name] into the expression window.
    2. Click OK.
  6. However, if the string field contains mixed alphanumeric and numeric values, a more detailed approach is required:
    1. With the Parser set to VB Script, and with Show Codeblock checked, copy-paste the code below into the Pre-Logic Script Code text window, and replace "String field name" below with the name of the field to be converted. Leave in the left and right brackets:
    2. Dim String, Check
      String = [String field name]
      Check = IsNumeric(String)
      If Check = True then
      Output = CDbl(String)
      else
      Output = null
      End if
      In the <Double field name> (in this example, "DoubleV =") expression window, type:
      Output
      
    3. The Field Calculator window looks like this:
      A picture of the code entered into the Field Calculator window.
    4. Click OK.
  7. Once the script completes, the Double field is populated with the values from the String field.
Note:
If a String field contains values with non-numeric characters or symbols, the value of that cell remains at the default of zero when converting to the Double field.

ArcGIS Pro 1.x

  1. Open an ArcGIS Pro project. Add the applicable data / table to the Project.
  2. Right-click the data / table, and select Attribute Table.
  3. Click the New button User-added image to create a new field. In the Fields list view, give the field a name, set the Data Type to Double, and ensure the Number Format is set to Numeric (and adjust the settings as desired). If necessary, set domain, default, and length values.
  4. Using the top ribbon, Save the edits to the table, and close the Fields tab.
  5. Right-click the header of the newly-created Double field, and select Calculate Field.  The Calculate Field Geoprocessing dialog opens.
  6. If <Null> (empty) values are in the String field, select a subset without them prior to performing the calculation, and type the following into the Expression text box:
    !<String field name>!
    Verify the expression, and Run the tool.
Note:
The tool may fail to run if there are Null (empty) values in the String field. Please read the ArcGIS Pro web help document, Create a new field and apply a domain and default value, for more information.

ArcView GIS 3.x

To convert a string field to a number field, create a new number field, and copy the values from the original field to the new field.
  1. Add a number field to the table.
    1. Open the table and select Start Editing from the Table menu.
    2. Select Add Field from the Edit menu.
    3. In the Field Definition dialog box:
      1. Set Name to an appropriate name, for example 'Area'.
      2. Set Type to Number.
      3. Set Width to an appropriate number, for example '32'.
      4. Set Decimal Places to the desired number, for example '6'.
      5. Click OK.
    4. Select Stop Editing from the Table menu and save the edits.
  2. Use the Field Calculator to copy the values to the new field and convert the values to a number.
    1. Open the table and Start Editing, if necessary.
    2. Select the Number field.
    3. Select Calculate from the Field menu.
    4. Double-click on the string field from the Fields list.
    5. Set the Type to String.
    6. Scroll down the Requests list and double-click AsNumber.
The final expression should look similar to this:
	[Strng_fld].AsNumber
  1. Click OK.
  2. Select Stop Editing from the Table menu and save the edits.

Related Information