* dilettante's function FmtDegMinSec() has rounding down error with Int()
* Function Convert_Degree() from http://support.microsoft.com/kb/213449 gives wrong format in some cases.
* This is my own function:Code:Function Convert_Degree(Decimal_Deg) As Variant With Application 'Set degree to Integer of Argument Passed Degrees = Int(Decimal_Deg) 'Set minutes to 60 times the number to the right 'of the decimal for the variable Decimal_Deg Minutes = (Decimal_Deg - Degrees) * 60 'Set seconds to 60 times the number to the right of the 'decimal for the variable Minute Seconds = Format(((Minutes - Int(Minutes)) * 60), "0") 'Returns the Result of degree conversion '(for example, 10.46 = 10~ 27 ' 36") Convert_Degree = " " & Degrees & "° " & Int(Minutes) & "' " _ & Seconds + Chr(34) End With End Function
Code:Function FormatDMS(ByVal Degs As Double) As String Degs = Int(Degs * 3600 + 0.5) / 3600 '-- rounding to second FormatDMS = Int(Degs) & Format(Degs / 24, "°nn'ss\""") End FunctionCode:Sub Test() Dim d As Double d = 134.9998 Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d) Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d) Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d) d = 134.9999 Debug.Print "Convert_Degree("; d; ") = "; Convert_Degree(d) Debug.Print " FmtDegMinSec("; d; ") = "; FmtDegMinSec(d) Debug.Print " FormatDMS("; d; ") = "; FormatDMS(d) End Sub Convert_Degree( 134.9998 ) = 134° 59' 59" FmtDegMinSec( 134.9998 ) = 134° 00' 59" FormatDMS( 134.9998 ) = 134°59'59" Convert_Degree( 134.9999 ) = 134° 59' 60" FmtDegMinSec( 134.9999 ) = 134° 00' 00" FormatDMS( 134.9999 ) = 135°00'00"




Reply With Quote