且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中将 DD(十进制度)转换为 DMS(度分秒)?

更新时间:2023-02-11 08:52:23

这正是 divmod 的发明目的:

>>>def decdeg2dms(dd):... mnt,sec = divmod(dd*3600,60)... deg,mnt = divmod(mnt,60)... 返回 deg,mnt,sec>>>dd = 45 + 30.0/60 + 1.0/3600>>>打印 dd45.5002777778>>>decdeg2dms(dd)(45.0, 30.0, 1.0)

How do you convert Decimal Degrees to Degrees Minutes Seconds In Python? Is there a Formula already written?

This is exactly what divmod was invented for:

>>> def decdeg2dms(dd):
...   mnt,sec = divmod(dd*3600,60)
...   deg,mnt = divmod(mnt,60)
...   return deg,mnt,sec

>>> dd = 45 + 30.0/60 + 1.0/3600
>>> print dd
45.5002777778
>>> decdeg2dms(dd)
(45.0, 30.0, 1.0)