Convert text to numbers with ArcGIS Arcade

Video

This tutorial is also available as a video.

Try to style the map with the Total Value field

You'll start by viewing the property assessment data on a map and reviewing the Total Value field. You'll attempt to symbolize the map with this field.

  1. Open the Portage la Prairie property assessment layer in Map Viewer.

    A map of the town's property boundaries appears.

    Map of property boundaries in Portage la Prairie, Manitoba, Canada

  2. If necessary, on the ribbon, click Sign In and sign in to your ArcGIS organizational account.
    Note:

    If you don't have an organizational account, see options for software access.

  3. In the Layers pane, on the layer name, click the Options button and click Show table.

    Options button and Show table option

    The layer's attribute table appears below the map.

  4. Scroll horizontally through the table to find the Total Value field.

    Total Value field in the table

    You'll visualize this field on the map so you can see which properties have higher values than others.

  5. Close the table.
  6. On the Settings toolbar on the right side of the screen, click the Styles button.

    Styles button

    Note:

    If the Settings toolbar is unavailable, in the Layers pane, click Portage la Prairie property assessment. The Settings toolbar is only available when a layer is selected.

  7. In the Styles pane, click the Field button.

    Field button

  8. In the Select fields window, click Total Value and click Add.

    The Styles pane updates to show styles that are available for the Total Value field. There are only two: Types (unique symbols) and Location (single symbol). You hoped to use a quantitative style such as Counts and Amounts (size), but none are available.

    Available styles

    Near the top of the Styles pane, next to Total Value, the field's type is listed as abc, indicating that it is a string, or text field.

    Field type indicator

    Numeric values are required to use the Counts and Amounts style. It won't work with text values.

Build an Arcade expression

You can still symbolize the map with the Total Value field and the Counts and Amounts style. You will use an Arcade expression to convert the text values into numeric values.

  1. In the Styles pane, next to Total Value, click the Remove button.

    Remove field button

  2. Click the Expression button.

    The expression builder window appears.

  3. Clear any sample code in the expression builder window.
  4. Near the bottom of the window, click the Expand button.

    Expand toolbar button

  5. In the expanded toolbar, click Functions.

    Functions button in the toolbar

    The Functions pane appears, listing all the available Arcade functions.

  6. In the search bar, type number.

    The list filters to one function, Number(value, pattern?) -> Number.

    • Number is the name of the function.
    • Value and pattern are its two parameters. The question mark indicates that pattern is an optional parameter.
    • The -> Number text at the end indicates that the output of this function will be a number.

      You'll use this function to convert the text values stored in the Total Value field into numeric values.

  7. Click Number(value, pattern?) -> Number.

    Number function

    The function appears in the expression builder. The value_ parameter is highlighted. Next, you'll replace it with the Total Value field, so leave the parameter highlighted.

    Number function with highlighted text

  8. On the toolbar, click Profile variables.

    Profile variables are data variables from the map that you can use as inputs in your Arcade expression. They include all of the fields from the layer.

    In the Profile variables pane, $feature refers to the features in the layer. In this case, the features are properties in the Portage la Prairie property assessment layer.

  9. Next to $feature, click the arrow button.

    Arrow button next to $feature

    A list of all of the fields from the Portage la Prairie property assessment layer appears.

  10. Scroll to the bottom of the list and click $feature.Total_Value.

    $feature.Total_Value

    The expression updates to Number ($feature.Total_Value).

    The first parameter, value, is now defined as $feature.Total_Value. The expression will access the Total Value attribute from each feature in the layer. The next parameter, pattern, is optional, so you'll run the expression to test if it works without defining a pattern.

  11. Above the expression builder, click Run.

    Run button

    In the Output window, the output Number: NaN is reported. NaN stands for Not a Number. In this case, the expression did not work with an undefined pattern.

  12. Edit the expression to Number($feature.Total_Value, '#.##').

    Edited expression

    The # character signifies an optional digit in the Number function. The #.## pattern will convert any amount of digits before the decimal and up to two digits after the decimal. The values in the Total Value field are monetary, so there's a possibility that some may be expressed with up to two decimal places. This pattern will ensure that values with decimal places can be converted.

  13. Click Run again.

    The Output window still reports Number: NaN. Next, you'll consult the function's documentation to find a solution.

Fix the expression

You recall from the attribute table that each number in the Total Value field was prefaced with a $ character. This is a text character. The expression is failing because it can't convert the $ character into a number. You'll consult the Number function's documentation to see if there's a way to remove the $ character.

  1. On the toolbar, click Functions.
  2. In the search bar, type number.
  3. Next to Number(value, pattern?) -> Number, click the arrow button.

    Arrow button next to Number function

    The function's documentation appears.

  4. Scroll down to the Examples section.

    The second example shows how to ignore certain characters when converting text to numbers.

    Number('abc10def', 'abc##def') // return 10.

    Example from the function documentation

    In the example expression, abc and def are defined as text that should be ignored. You can use this pattern to ignore the $ character.

  5. In the expression builder, place your cursor in front of #.## and type $.

    The final expression should read Number($feature.Total_Value, '$#.##').

  6. Click Run.

    The Output window reports Number: 85200. The expression was successful in converting values like $85,200 into numbers like 85200.

    Output

    Note:

    If your web browser is set to a language other than American English, the expression may fail due to the commas used as thousands separators. If the Output window still reports Number: NaN, erase the existing expression and replace it with the following:

    var numberOnly = Replace($feature.Total_Value, '$', '') // Remove dollar signs.
    var digitsOnly = Replace(numberOnly, ',', '') // Remove commas.
    Number(digitsOnly)
    Note:

    When you click Run, the expression is tested against the first feature in the layer.

    Now that the expression is complete. you'll change its name to better describe its purpose.

  7. At the top of the window, click New expression. Erase the existing text and type Total Value (numeric).

    Renamed expression

  8. Click Done.

    The map reappears. The Styles pane lists the Total Value (numeric) expression as the chosen attribute. Counts and Amounts (color) has been selected as the default style for the numeric values returned by the expression.

    Map and styles pane

    The map visualizes the values from Total Value field with larger circles for larger assessed property values.

    Note:

    The Total Value field and the Portage la Prairie property assessment layer are unchanged. The Total Value (numeric) expression is saved in the web map, not in the layer, but if you save a copy of the layer, it will be available for use in other maps.

In this tutorial, you learned how to use the Arcade Number function to convert text values to numeric values for a layer in a map so you could apply the Counts and Amounts style. The expression you wrote—Number($feature.Total_Value, '$#.##')—can also be used to format pop-ups and labels.

You can find more tutorials in the tutorial gallery.