Convert DD (decimal degrees) to DMS (degrees minutes seconds) in Python?

David picture David · Apr 5, 2010 · Viewed 32.7k times · Source

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

Answer

PaulMcG picture PaulMcG · Apr 5, 2010

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)