度分秒转换函数

调用例子

也可以用于小时、分钟、秒的转换,但中间的分隔符只能是一位

MsgBox DToDMS(1.51, "度分秒")
MsgBox DMSToD("12度3分4秒")
MsgBox DMSToD("12h3m4.56s")
MsgBox DMSToD("12°3'4"")

度分秒转度

支持度分秒之间使用任意非数字字符分隔,支持度分秒中存在小数

Function DMStoD(DMS As String)
Dim sp(2) As Single
Dim i As Integer
Dim sta As Integer

On Error Resume Next
DMS = Trim(DMS)
Do While IsNumeric(Mid(DMS, i, 1)) Or Mid(DMS, i, 1) = "."
    i = i + 1
Loop

sp(0) = Left(DMS, i - 1)
Do Until IsNumeric(Mid(DMS, i, 1)) Or i > Len(DMS)
    i = i + 1
Loop
If i <= Len(DMS) Then
    sta = i
    Do While IsNumeric(Mid(DMS, i, 1)) Or Mid(DMS, i, 1) = "."
        i = i + 1
    Loop
    sp(1) = Mid(DMS, sta, i - sta)
    Do Until IsNumeric(Mid(DMS, i, 1)) Or i > Len(DMS)
        i = i + 1
    Loop
    If i <= Len(DMS) Then
        sta = i
        Do While IsNumeric(Mid(DMS, i, 1)) Or Mid(DMS, i, 1) = "."
            i = i + 1
        Loop
        sp(2) = Mid(DMS, sta, i - sta)
    End If
End If
DMStoD = Round(sp(0) + sp(1) / 60 + sp(2) / 3600, 4)
End Function

度转度分秒

支持自定义分隔符,支持自定义秒的小数位数

Function DToDMS(D As Single, Optional SpStr As String = "°'""", Optional Deci As Long = 0)
Dim sp(2) As String
sp(0) = Int(D)
sp(1) = Int((D - sp(0)) * 60)
sp(2) = Round((D - sp(0) - sp(1) / 60) * 60 * 60, Deci)
DToDMS = sp(0) & Mid(SpStr, 1, 1) & sp(1) & Mid(SpStr, 2, 1) & sp(2) & Mid(SpStr, 3, 1)
End Function

——专注办公软件的二次开发及培训,你有问题,我有思路!
——微博、微信、CSDN同号:w_dexu
——转载请注明出处!

微信二维码扫码加微信

猜你喜欢

转载自blog.csdn.net/w_dexu/article/details/107240287