<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do you convert from numbers as text string to numberic, double, long, etc? in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157762#M5299</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I know how to do it in Avenue and I see old threads about how to do it in VBA.&amp;nbsp; But how do I do it now with VB or Python?&amp;nbsp; This should be simple but every time I get an error.&amp;nbsp; I have one column with numbers and its a String field.&amp;nbsp; 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.&amp;nbsp; How?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; The basic calculation with VB in the Field Calculator is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Parser: VB Script&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use codeblock:&amp;nbsp; Checked&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-Logic Script Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;If IsNumeric([String_Field]) Then &amp;nbsp; Output = CLng(Trim([String_Field])) ' Or use Cdbl for double Else &amp;nbsp; Output = [Number_Field] ' This will keep Null values.&amp;nbsp; Or use 0 or -1 End If&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&amp;nbsp; Output&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 11 Jun 2014 23:34:14 GMT</pubDate>
    <dc:creator>RichardFairhurst</dc:creator>
    <dc:date>2014-06-11T23:34:14Z</dc:date>
    <item>
      <title>How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157761#M5298</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I know how to do it in Avenue and I see old threads about how to do it in VBA.&amp;nbsp; But how do I do it now with VB or Python?&amp;nbsp; This should be simple but every time I get an error.&amp;nbsp; I have one column with numbers and its a String field.&amp;nbsp; 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.&amp;nbsp; How?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jun 2014 22:27:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157761#M5298</guid>
      <dc:creator>TomMagdaleno</dc:creator>
      <dc:date>2014-06-11T22:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157762#M5299</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I know how to do it in Avenue and I see old threads about how to do it in VBA.&amp;nbsp; But how do I do it now with VB or Python?&amp;nbsp; This should be simple but every time I get an error.&amp;nbsp; I have one column with numbers and its a String field.&amp;nbsp; 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.&amp;nbsp; How?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; The basic calculation with VB in the Field Calculator is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Parser: VB Script&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use codeblock:&amp;nbsp; Checked&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-Logic Script Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;If IsNumeric([String_Field]) Then &amp;nbsp; Output = CLng(Trim([String_Field])) ' Or use Cdbl for double Else &amp;nbsp; Output = [Number_Field] ' This will keep Null values.&amp;nbsp; Or use 0 or -1 End If&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Expression:&amp;nbsp; Output&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jun 2014 23:34:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157762#M5299</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-06-11T23:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157763#M5300</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jun 2014 23:41:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157763#M5300</guid>
      <dc:creator>TomMagdaleno</dc:creator>
      <dc:date>2014-06-11T23:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157764#M5301</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;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!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FYI.&amp;nbsp; 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.&amp;nbsp; 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.&amp;nbsp; You can also change the field name if you like.&amp;nbsp; This maintains the original field position in the schema and does the actual conversion of the string values.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jun 2014 23:45:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157764#M5301</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-06-11T23:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157765#M5302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Richard.&amp;nbsp; I needed to add zeros in front of the numbers less than three digits long.&amp;nbsp; 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.&amp;nbsp; But your tips for finding blanks will be very useful in the future too.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jun 2014 23:49:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157765#M5302</guid>
      <dc:creator>TomMagdaleno</dc:creator>
      <dc:date>2014-06-11T23:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert from numbers as text string to numberic, double, long, etc?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157766#M5303</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks Richard.&amp;nbsp; I needed to add zeros in front of the numbers less than three digits long.&amp;nbsp; 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.&amp;nbsp; But your tips for finding blanks will be very useful in the future too.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If that is the end goal then you can do a python calculation like this on the string field:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Parser: Python&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use codeblock: Checked&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pre-logic Script Code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def AddZeros(strFld):
&amp;nbsp;&amp;nbsp;&amp;nbsp; If strFld.isnumeric():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; longNumber = round(float(strFld), 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; longNumber = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; return "%(num)04.0f" % {'num':longNumber}&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The 04.0f adds leading zeros to any number less than 4 digits long.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:21:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/how-do-you-convert-from-numbers-as-text-string-to/m-p/157766#M5303</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T08:21:10Z</dc:date>
    </item>
  </channel>
</rss>

