dmetzler

Define precision of Number to Text in Python

Discussion created by dmetzler on Jan 24, 2013
Latest reply on Jan 24, 2013 by dmetzler
Greetings,
    I am attempting to calc a LAT and LONG that is in DD to new fields that are DMS. I have the python below that does the trick however I cannot define the precision of the Seconds. So the return ranges from three to 8 decimal places. I would love to use the calculate geometry feature but I need the conversion to be mathmatic based on the attribute. I am also aware of the "Convert Coordinate Notation???  tool but I would like to avoid the join and output process.

Here is the Python
def decdeg2dms(dd):
    negative = dd < 0
    dd = abs(dd)
    minutes,seconds =  divmod(dd*3600,60)
    degrees,minutes =   divmod(minutes,60)
    if negative:
        if degrees > 0:
            degrees = -degrees
        elif minutes > 0:
            minutes = -minutes
        else:
            seconds = -seconds
            seconds = round(seconds, 5) #my attempt to define it
   
    dms= str(int(degrees)) + " " + str(int(minutes)) + "' " + str(seconds) + "''"
    
    return (dms)


and the Exicution:
decdeg2dms ( !GlobalLati! )



As I mentioned before the precision is all over the place, any comments would be appriciated

Thanks and have a wonderful day!

Outcomes