如何在 Python 中将十进制度数转换为度分秒?是否已经编写了公式?
最佳答案
这正是 divmod
的发明目的:
def decdeg2dms(dd):
mult = -1 if dd < 0 else 1
mnt,sec = divmod(abs(dd)*3600, 60)
deg,mnt = divmod(mnt, 60)
return mult*deg, mult*mnt, mult*sec
dd = 45 + 30/60 + 1/3600
print(decdeg2dms(dd))
# negative value returns all negative elements
print(decdeg2dms(-122.442))
打印:
(45.0, 30.0, 1.0)
(-122.0, -26.0, -31.199999999953434)
关于python - 在 Python 中将 DD(十进制度)转换为 DMS(度分秒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2579535/