Convert DMS to DD

Viewed 20

I would like to convert decimal degrees into sexagesimal degrees, at the level of calculations I based myself on this site and this is my code

def dec_dms(deg, dec):
    mnt = dec * 60
    dec = mnt%1
    mnt = mnt - dec
    dec = dec * 60
    return deg, mnt, dec

print(dec_dms(12,345))

>>>(12, 20700, 0)

the problem is the result which should be (12, 20, 42) Can you help me please ?

1 Answers

Your code is supposed to work with the decimal part of the degree. So for the decimal degree 12.345, you should multiply 60 by the decimal part which should be 0.345 and not 345

So either you send 0.345 as parameter to your function or you add a leading 0. to your number. Here is a way to do the latter :

def dec_dms(deg, dec):
    dec = float('.' + str(dec))
    mnt = dec * 60
    dec = mnt % 1
    mnt = mnt - dec
    dec = dec * 60
    return deg, round(mnt), round(dec)

print(dec_dms(12, 345))

output:

(12, 20, 42)
Related