|
- Private Function DecToBin(ByVal nNum As Double) As String
- Dim nInt As Double, nSmall As Double
- Dim sStr As String, nMod As Integer
- Dim nBit As Integer
-
- Set oDic = CreateObject("Scripting.Dictionary")
- nInt = Int(nNum)
- nSmall = Round(nNum - nInt, Len(Trim(nNum)) - Len(Trim(nInt)))
- '整数部分
- Do While nInt > 0
- sStr = (nInt Mod 2) & sStr
- nInt = Int(nInt / 2)
- Loop
- '整数部分结束
- '小数部分
- If nSmall > 0 Then
- sStr = sStr & "."
- Do While nSmall > 0
- nBit = nBit + 1
- nSmall = nSmall * 2
- nInt = Int(nSmall)
- nSmall = nSmall - nInt
- sStr = sStr & nInt
- If nBit = 100 Then Exit Do
- Loop
- End If
- If nBit = 100 Then sStr = sStr & "……"
- '小数部分结束
- DecToBin = sStr
- End Function
- Sub 计算()
- Dim sBin As String
- sBin = DecToBin(100)
- End Sub
复制代码 |
|