简体   繁体   中英

Positive and negative values in fields

I want to make if field1 is a negative integer then field 2 will be the same integer but positive. but if field1 is any positive then field2 will be always 0.0

Example:

if
 field1 = -180:
     field2 = 180
if field1 = 180
    field2 = 0.0

You can check if a number is negative by comparing it to zero.

if field1 < 0:
    field2 = -field1
else:
    field2 = 0.0

Or more succinctly:

field2 = (-field1 if field1 < 0 else 0.0)

try this:

if field1 < 0:
    field2 = abs(field1)
else:
    field2 = 0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM