Search code examples
pythondecimalseparator

Problems with decimal separator


I've latitude/longitude numbers, like: -4726786457893145

but i need a decimal separator, like: -47.26786457893145

I've tried 'format' or 'for' structures but without success. I've even converted to list, then inserted a dot and then convert to float again, but not work for all numbers and i do not know why. I'd appreciate your help. Thanks.

Here's my partial code:

latitude = []
longitude = []
for i in range(len(data['Latitude'])):
    a = list(str(data['Latitude'][i].replace(',','')))
    a.insert(3,'.')
    str1 = ''.join(a)
    latitude.append(float(str1))

    a = list(str(data['Longitude'][i].replace(',','')))
    a.insert(3,'.')
    str1 = ''.join(a)
    longitude.append(float(str1))

Solution

  • This function which I made... which could be written more clearly... will turn -4726786457893145 into -47.26786457893145.

    def decimal_add(n):
        if n < 0:
            sign = -1
            int_part = float(str(n)[1:3])
            decimal_part = float(str(n)[3:])/10**(len(str(n))-3)
    
    
        else:
            sign = 1
            int_part = float(str(n)[0:2])
            decimal_part = float(str(n)[2:])/10**(len(str(n))-2)
        number = (int_part+decimal_part) * sign
        return number
    

    Examples from jupyter notebook:

    enter image description here