Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Define precision of Number to Text in Python

$
0
0
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
Code:

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:
Code:

decdeg2dms ( !GlobalLati! )

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

Thanks and have a wonderful day!

Viewing all articles
Browse latest Browse all 2485

Trending Articles