Home > OS >  How can this function can be optimized to convert decimal degrees to integer degrees and decimal min
How can this function can be optimized to convert decimal degrees to integer degrees and decimal min

Time:12-15

There is the module for excel which converts decimal degrees into DD.MM.SS, but I want it to convert to DD.MM.MMM (degrees and decimal minutes)

Function Convert_Degree(Decimal_Deg) As Variant
    With Application
        Degrees = Int(Decimal_Deg)
        Minutes = (Decimal_Deg - Degrees) * 60
        Seconds = Format(((Minutes - Int(Minutes)) * 60), "0")
        Convert_Degree = " " & Degrees & "° " & Int(Minutes) & " ' " & Seconds   Chr(34)
    End With
End Function

As I am not a programmer, I don't know how to make it works Thanks for helping =)

CodePudding user response:

Would this do the trick?

Function Convert_Degree(Decimal_Deg) As Variant
    With Application
        Degrees = Int(Decimal_Deg)
        Minutes = (Decimal_Deg - Degrees) * 60
        Convert_Degree = " " & Degrees & "° " & Format(Minutes,"0.0000") & " ' "
    End With
End Function

Change the "0.0000" to include as many or as few 0's as you need for your precision.

CodePudding user response:

Function Convert_Degree(Decimal_Deg) As String
Dim intDeg As Long
Dim decMin As Single

    intDeg = Int(Decimal_Deg)
    decMin = Decimal_Deg - intDeg
    
'    Convert_Degree = intDeg & Chr$(176) & " " & Format(decMin, "0.000") & "'"  ' D° M.MMMM'
'    Convert_Degree = intDeg & "." & Format(decMin, "00.000")   ' D.MM.MMM

    Convert_Degree = Right$("0" & intDeg, 2) & "." & Format(decMin, "00.000") ' DD.MM.MMM
End Function
  • Related