简体   繁体   English

确定一个数字中的千分之一

[英]Determining Thousandths in a number

If an aircraft is flying VFR in the US, if the heading is east, the altitude must be an odd thousand plus 500 feet (1500, 3500, 5500, etc). 如果飞机在美国飞行VFR,如果航向为东,则海拔高度必须是千零加500英尺(1500,3500,5500等)。 If flying west, the altitude must be an even thousand plus 500 feet (2500, 4500, 6500, etc). 如果向西飞行,海拔必须是一千甚至五千英尺(2500,4500,6500等)。 If I input a given altitude, but it is the wrong (odd or even) for the heading, how do I get Python to correct it next higher odd or even thousandths (1500 becomes 2500, 6500 becomes 7500, etc)? 如果我输入一个给定的高度,但是标题是错误的(奇数或偶数),我如何让Python在下一个更高的奇数或偶数千分之一(1500变为2500,6500变为7500等)时进行纠正? We never round down for altitudes. 我们永远不会向下舍入高度。 Thanks! 谢谢!

You could divide your altitude by 1000.0 and cast to an int which would drop the decimal: 您可以将高度除以1000.0并转换为可以删除小数的int:

if int(altitude/1000.0) % 2 == 0

Then you can do whatever you want with that information. 然后,您可以使用该信息做任何您想做的事情。

You can use math.ceil to do this: 您可以使用math.ceil执行此操作:

>>> import math
>>> def next_alt(alt):
...    return (math.ceil(alt/1000)+1)*1000+500
... 
>>> next_alt(2500)
3500.0
>>> next_alt(3500)
4500.0

Which could then be used in a function this way: 然后可以通过这种方式在函数中使用:

def set_alt(heading, alt):
    if 0<=heading<=179:       # odd + 500
        return alt if alt / 1000 % 2 else (math.ceil(alt/1000)+1)*1000+500
    else:
        return alt if not alt / 1000 % 2 else (math.ceil(alt/1000)+1)*1000+500

(If Python 3, you need // instead of / ) (如果是Python 3,则需要//而不是/

>>> set_alt(290, 3500)
4500
>>> set_alt(90, 3500)
3500

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

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