Python - Negative value with field Calculator

6057
13
Jump to solution
07-24-2012 05:47 AM
Pierre-LucBoivin
Occasional Contributor
Hi,

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â??â??.

[ATTACH=CONFIG]16352[/ATTACH]

Variation column is the result I get with this formula :

def Resultat(x,y):
 diff =  x-y
 return '%d:%02d' % divmod(diff, 60)

Variation = Resultat( !ScePUPbr! , !SitPUPbr!)


Testvari column is the result if I reverse the values â??â??xy

Testvari2 column is the result I should get

Thanks you for help

Pierre-Luc
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Pierre-LucBoivin
Occasional Contributor
Here the final solution !

def Resultat(x,y):
 diff =  x-y
 if diff < 0:
  sign = '-'
 else:
  sign = ''
 return sign+'%d:%02d' % divmod(abs(diff), 60)


Thanks everyone for your help

View solution in original post

0 Kudos
13 Replies
SolomonPulapkura
Occasional Contributor III
What expression are you using to calculate Testvari2?

The expression you provided is for Variation.
0 Kudos
Pierre-LucBoivin
Occasional Contributor
I populate the field manually using the field calculator !!
0 Kudos
BruceNielsen
Occasional Contributor III
Take the absolute value of the difference:
diff = abs(x-y)
0 Kudos
Pierre-LucBoivin
Occasional Contributor
Take the absolute value of the difference:
diff = abs(x-y)


I've tried this solution but it's isn't returning the minus before my number !
0 Kudos
BruceNielsen
Occasional Contributor III
This should add the negative sign, if needed:
def Resultat(x,y):
    diff =  x-y
    if diff < 0:
        sign = '-'
    else:
        sign = '' #These are two single quotes (an empty string)
    return '%s%d:%02d' % (sign, divmod(diff, 60))
0 Kudos
Pierre-LucBoivin
Occasional Contributor
Thanks a lot Bruce for helping !

I think we are almost there but i'm still having an error !!

ERROR 000539: Error running expression: Resultat( -2000 , 0) <type 'exceptions.TypeError'>: %d format: a number is required, not tuple


I tried to fix the error but it wasn't successful .
0 Kudos
BruceNielsen
Occasional Contributor III
We'll try it without using string formatting for the sign then:
def Resultat(x,y):
    diff =  x-y
    if diff < 0:
        sign = '-'
    else:
        sign = '' #These are two single quotes (an empty string)
    return sign + '%d:%02d' % divmod(diff, 60)
0 Kudos
Pierre-LucBoivin
Occasional Contributor
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)

I'm try to put the abs() but it was evident it wouldn't work.

def Resultat(x,y):
    diff =  abs(x-y) #abs doesn't make any difference without a positive and negative value
    if diff < 0:
        sign = '-'
    else:
        sign = '' #These are two single quotes (an empty string)
    return sign + '%d:%02d' % divmod(diff, 60)


is it because of the %02d that it always calculate the difference from 2:00 ?
0 Kudos
BruceNielsen
Occasional Contributor III
If you go back to your original formula, but set diff = y - x, does that give you the results you expect?
0 Kudos