How do you convert from numbers as text string to numberic, double, long, etc?

2557
5
Jump to solution
06-11-2014 03:27 PM
TomMagdaleno
Occasional Contributor III
I know how to do it in Avenue and I see old threads about how to do it in VBA.  But how do I do it now with VB or Python?  This should be simple but every time I get an error.  I have one column with numbers and its a String field.  I added a new field as a Long and am trying to use the field calculator to populate it with the same data as the string field, but as a number.  How?
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
I know how to do it in Avenue and I see old threads about how to do it in VBA.  But how do I do it now with VB or Python?  This should be simple but every time I get an error.  I have one column with numbers and its a String field.  I added a new field as a Long and am trying to use the field calculator to populate it with the same data as the string field, but as a number.  How?


You need to test each string to verify it is numeric and if it is not replace it with either a 0, -1, or Null, depending on how you want to treat invalid values.  The basic calculation with VB in the Field Calculator is:

Parser: VB Script

Use codeblock:  Checked

Pre-Logic Script Code:
If IsNumeric([String_Field]) Then   Output = CLng(Trim([String_Field])) ' Or use Cdbl for double Else   Output = [Number_Field] ' This will keep Null values.  Or use 0 or -1 End If


Expression:  Output

View solution in original post

0 Kudos
5 Replies
RichardFairhurst
MVP Honored Contributor
I know how to do it in Avenue and I see old threads about how to do it in VBA.  But how do I do it now with VB or Python?  This should be simple but every time I get an error.  I have one column with numbers and its a String field.  I added a new field as a Long and am trying to use the field calculator to populate it with the same data as the string field, but as a number.  How?


You need to test each string to verify it is numeric and if it is not replace it with either a 0, -1, or Null, depending on how you want to treat invalid values.  The basic calculation with VB in the Field Calculator is:

Parser: VB Script

Use codeblock:  Checked

Pre-Logic Script Code:
If IsNumeric([String_Field]) Then   Output = CLng(Trim([String_Field])) ' Or use Cdbl for double Else   Output = [Number_Field] ' This will keep Null values.  Or use 0 or -1 End If


Expression:  Output
0 Kudos
TomMagdaleno
Occasional Contributor III
Richard, If your ever down in Camarillo stop by, I owe you a big steak dinner for all the help you've provided for me time and again!
0 Kudos
RichardFairhurst
MVP Honored Contributor
Richard, If your ever down in Camarillo stop by, I owe you a big steak dinner for all the help you've provided for me time and again!


FYI.  If yo don't mind creating a new feature class, do the calculation on the String field itself and replace invalid values with 0 or -1 so that you are sure all values are numeric.  Then use the Feature Class to Feature Class tool and change the field type of the string in the field list to Long or Double.  You can also change the field name if you like.  This maintains the original field position in the schema and does the actual conversion of the string values.
0 Kudos
TomMagdaleno
Occasional Contributor III
Thanks Richard.  I needed to add zeros in front of the numbers less than three digits long.  By putting them in a number field I could sort them, then select the ones in the text field in one swoop, instead of hunting and pecking for them with the odd sorting of text fields.  But your tips for finding blanks will be very useful in the future too.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks Richard.  I needed to add zeros in front of the numbers less than three digits long.  By putting them in a number field I could sort them, then select the ones in the text field in one swoop, instead of hunting and pecking for them with the odd sorting of text fields.  But your tips for finding blanks will be very useful in the future too.


If that is the end goal then you can do a python calculation like this on the string field:

Parser: Python

Use codeblock: Checked

Pre-logic Script Code:
def AddZeros(strFld):
    If strFld.isnumeric():
        longNumber = round(float(strFld), 0)
    else:
        longNumber = 0
    return "%(num)04.0f" % {'num':longNumber}


The 04.0f adds leading zeros to any number less than 4 digits long.
0 Kudos