<?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: Python - Negative value with field Calculator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315334#M24516</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Take the absolute value of the difference:&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;diff = abs(x-y)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've tried this solution but it's isn't returning the minus before my number !&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 25 Jul 2012 18:32:48 GMT</pubDate>
    <dc:creator>Pierre-LucBoivin</dc:creator>
    <dc:date>2012-07-25T18:32:48Z</dc:date>
    <item>
      <title>Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315330#M24512</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to subtract two integer columns and put the result into a text field. I got good result with positive valuesâ??â??, but I have a problem with negative valuesâ??â??.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]16352[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Variation column is the result I get with this formula :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def Resultat(x,y):
 diff =&amp;nbsp; x-y
 return '%d:%02d' % divmod(diff, 60)

Variation = Resultat( !ScePUPbr! , !SitPUPbr!)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Testvari column is the result if I reverse the values â??â??xy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Testvari2 column is the result I should get&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks you for help&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre-Luc&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:20:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315330#M24512</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2021-12-12T16:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315331#M24513</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What expression are you using to calculate Testvari2?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The expression you provided is for Variation.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jul 2012 03:43:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315331#M24513</guid>
      <dc:creator>SolomonPulapkura</dc:creator>
      <dc:date>2012-07-25T03:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315332#M24514</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I populate the field manually using the field calculator !!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jul 2012 14:04:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315332#M24514</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2012-07-25T14:04:52Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315333#M24515</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Take the absolute value of the difference:&lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;diff = abs(x-y)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jul 2012 16:58:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315333#M24515</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-07-25T16:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315334#M24516</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Take the absolute value of the difference:&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;diff = abs(x-y)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I've tried this solution but it's isn't returning the minus before my number !&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jul 2012 18:32:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315334#M24516</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2012-07-25T18:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315335#M24517</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This should add the negative sign, if needed:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def Resultat(x,y):
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; x-y
&amp;nbsp;&amp;nbsp;&amp;nbsp; if diff &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '-'
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '' #These are two single quotes (an empty string)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return '%s%d:%02d' % (sign, divmod(diff, 60))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:00:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315335#M24517</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-11T15:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315336#M24518</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot Bruce for helping !&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think we are almost there but i'm still having an error !!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;ERROR 000539: Error running expression: Resultat( -2000 , 0) &amp;lt;type 'exceptions.TypeError'&amp;gt;: %d format: a number is required, not tuple&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to fix the error but it wasn't successful .&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 11:15:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315336#M24518</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2012-07-26T11:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315337#M24519</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;We'll try it without using string formatting for the sign then:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Resultat(x,y):
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; x-y
&amp;nbsp;&amp;nbsp;&amp;nbsp; if diff &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '-'
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '' #These are two single quotes (an empty string)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sign + '%d:%02d' % divmod(diff, 60)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:00:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315337#M24519</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-11T15:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315338#M24520</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I thought this code will work too but it returning the same value as the Variation column but I have two minus before the number ex:(--1:56)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm try to put the abs() but it was evident it wouldn't work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Resultat(x,y):
&amp;nbsp;&amp;nbsp;&amp;nbsp; diff =&amp;nbsp; abs(x-y) #abs doesn't make any difference without a positive and negative value
&amp;nbsp;&amp;nbsp;&amp;nbsp; if diff &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '-'
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sign = '' #These are two single quotes (an empty string)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return sign + '%d:%02d' % divmod(diff, 60)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;is it because of the %02d that it always calculate the difference from 2:00 ?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:00:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315338#M24520</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2021-12-11T15:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315339#M24521</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you go back to your original formula, but set &lt;/SPAN&gt;&lt;STRONG&gt;diff = y - x&lt;/STRONG&gt;&lt;SPAN&gt;, does that give you the results you expect?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 15:19:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315339#M24521</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-07-26T15:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315340#M24522</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well still doesn't work ! I'm gonna populate my field testvari2 with an selection and concatenation of the column Variation and testVari !!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For sure, this is not an optimal solution but it'll work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;!Variation![0:1] + !testvari!&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 16:18:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315340#M24522</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2012-07-26T16:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315341#M24523</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I guess I am not understanding the value you are trying to obtain - subtracting a smaller number from a larger number will always return a positive value, and it stands to reason that nearly any action taken using that value will also return another positive value. Absolute value doesn't work because by definition absolute value returns a positive value, it doesn't simply reverse the sign.&amp;nbsp;&amp;nbsp; Now, if the minus sign is simply symbolic of the the difference between the two numbers, then thats somewhat different though misleading.&amp;nbsp; What was the expression you put into the field calculator to obtain the negative value?&amp;nbsp; If you're set on getting a negative value there then this will do it i think:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;'-%d:%02d' % divmod(4, 60)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 17:14:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315341#M24523</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-07-26T17:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315342#M24524</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I absolutely need the negative value because i'm calculating difference between 2 streets network for Emergency travel time. So I need to know the variation(negative or positive) between the different network.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 26 Jul 2012 17:24:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315342#M24524</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2012-07-26T17:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Negative value with field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315343#M24525</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here the final solution !&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def Resultat(x,y):
 diff =&amp;nbsp; x-y
 if diff &amp;lt; 0:
&amp;nbsp; sign = '-'
 else:
&amp;nbsp; sign = ''
 return sign+'%d:%02d' % divmod(abs(diff), 60)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks everyone for your help&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:20:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-negative-value-with-field-calculator/m-p/315343#M24525</guid>
      <dc:creator>Pierre-LucBoivin</dc:creator>
      <dc:date>2021-12-12T16:20:05Z</dc:date>
    </item>
  </channel>
</rss>

