简体   繁体   English

字段中的正值和负值

[英]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. 我想让,如果field1是一个负整数,然后字段2将是相同的整数,但为正。 but if field1 is any positive then field2 will be always 0.0 但是,如果field1为正,则field2始终为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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