def decimal2dms(decDeg, delimiter):
    """Converts decimal degrees to string in Degrees:Minutes:Seconds format
    with user specified delimiter.

    @type decDeg: float
    @param decDeg: coordinate in decimal degrees
    @type delimiter: string
    @param delimiter: delimiter character in returned string
    @rtype: string
    @return: coordinate string in D:M:S format

    """
    if decDeg > 0:
        if 1 <= decDeg < 10:
            sDeg = '0' + str(decDeg)[0]
        elif decDeg >= 10:
            sDeg = str(decDeg)[:2]
        elif decDeg < 1:
            sDeg = '00'
        else:
            return 'nan'
        if str(decDeg).find('.') == -1:
            mins = float(decDeg) * 60.0
        else:
            mins = float(str(decDeg)[str(decDeg).index('.'):]) * 60
        if 1 <= mins < 10:
            sMins = '0' + str(mins)[:1]
        elif mins >= 10:
            sMins = str(mins)[:2]
        elif mins < 1:
            sMins = '00'
        else:
            return 'nan'
        secs = (decDeg - (float(sDeg) + float(sMins) / 60.0)) * 3600.0
        if 0 < secs < 10:
            sSecs = '0' + str(secs)[:str(secs).find('.') + 3]
        elif secs < 0.001:
            sSecs = '00.00'
        else:
            sSecs = str(secs)[:str(secs).find('.') + 3]
        if len(sSecs) < 5:
            sSecs = sSecs + '0'
        if float(sSecs) == 60.0:
            sSecs = '00.00'
            sMins = str(int(sMins) + 1)
        if int(sMins) == 60:
            sMins = '00'
            sDeg = str(int(sDeg) + 1)
        return '+' + sDeg + delimiter + sMins + delimiter + sSecs
    else:
        if -10 < decDeg <= -1:
            sDeg = '-0' + str(decDeg)[1]
        elif decDeg <= -10:
            sDeg = str(decDeg)[:3]
        elif decDeg > -1:
            sDeg = '-00'
        else:
            return 'nan'
        if str(decDeg).find('.') == -1:
            mins = float(decDeg) * -60.0
        else:
            mins = float(str(decDeg)[str(decDeg).index('.'):]) * 60
        if 1 <= mins < 10:
            sMins = '0' + str(mins)[:1]
        elif mins >= 10:
            sMins = str(mins)[:2]
        elif mins < 1:
            sMins = '00'
        else:
            return 'nan'
        secs = (decDeg - (float(sDeg) - float(sMins) / 60.0)) * 3600.0
        if -10 < secs < 0:
            sSecs = '0' + str(secs)[1:str(secs).find('.') + 3]
        elif secs > -0.001:
            sSecs = '00.00'
        else:
            sSecs = str(secs)[1:str(secs).find('.') + 3]
        if len(sSecs) < 5:
            sSecs = sSecs + '0'
        if float(sSecs) == 60.0:
            sSecs = '00.00'
            sMins = str(int(sMins) + 1)
        if int(sMins) == 60:
            sMins = '00'
            sDeg = str(int(sDeg) - 1)
        return sDeg + delimiter + sMins + delimiter + sSecs
