发表于:2004-04-26 10:35:00
2楼
Public Function Bin(ByVal n As Double, ByVal m As Long) As String
Dim i As Long, dot As Long, iP As Long, fP As Double
Dim prefix As String, BinInt As String, BinFloat As String
If Left(n, 1) = "-" Then prefix = "-": n = Mid(n, 2)
dot = InStr(n, ".")
If dot <> 0 Then iP = Left(n, dot - 1): fP = Mid(n, dot) Else iP = n
Do
BinInt = (iP Mod 2) & BinInt
iP = iP \ 2
Loop Until iP = 0
BinInt = prefix & BinInt
If dot = 0 Then Bin = BinInt: Exit Function
For i = 1 To m
fP = fP * 2
fP = (fP - Int(fP)) + (Int(fP) Mod 2)
BinFloat = BinFloat & Int(fP)
If fP = 1 Then Exit For
Next
Bin = BinInt & "." & BinFloat
End Function
Public Function Dec(ByVal n As String) As Double
Dim i As Long, j As Long, dot As Long, prefix As Long
prefix = Sgn(n)
If prefix = -1 Then n = Mid(n, 2)
dot = InStr(n, ".")
If dot = 0 Then
dot = Len(n) - 1
Else
n = Left(n, dot - 1) & Mid(n, dot + 1)
dot = dot - 2
End If
For i = dot To dot - Len(n) + 1 Step -1
j = j + 1
If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ i
Next
Dec = Dec * prefix
End Function
仅供参考