在Visual Basic中将实数转换为十进制

时间:2012-07-02 22:19:52

标签: vb.net algorithm

你们可以帮忙吗?我需要将存储为(x.y)的实数转换为visual basic中的十进制值,反之亦然。

这个过程中的任何psudocode / code / answers都会很棒,欢呼! 马丁

2 个答案:

答案 0 :(得分:2)

Dim realNumber = 123.456
Dim decimalValue = Convert.ToDecimal(realNumber)
realNumber = Decimal.ToDouble(decimalValue)

答案 1 :(得分:-1)

'只需调用.... REAL_to_DOUBLE(1112573452)或REAL_to_DOUBLE(4742453279273779200)

 Public Function REAL_to_DOUBLE(ByVal i3264 As Long) 
            Dim sBit As Integer = 1
            Dim expBits As Integer = 8 'for 32 bits, 11 for 64 bits
            Dim expAux As Integer = 127 'for 32 bts, 1023 for 64 bits
            Dim nBits As Integer = 32 ' for 23 bits, 64 for 64 bits
            Dim dec As Double = 1

            Dim hexstring As String = Hex(i3264)

            If hexstring.Length > 8 Then
                expBits = 11
                nBits = 64
                expAux = 1023
            End If

            Dim bin_ As String
            If nBits = 32 Then
                bin_ = Convert.ToString(Convert.ToInt32(hexstring, 16), 2).PadLeft(nBits, "0"c)
            Else
                bin_ = Convert.ToString(Convert.ToInt64(hexstring, 16), 2).PadLeft(nBits, "0"c)
            End If
            Dim _sinal As Integer = -1
            If (bin_.Substring(0, 1)) = "0" Then _sinal = 1

            Dim _e As String = bin_.Substring(1, expBits).PadLeft(expBits, "0"c)
            Dim a As Integer = Convert.ToInt32(_e, 2)
            Dim exp_ As Integer = a - expAux

            Dim matissa As String = bin_.Substring(expBits + 1, bin_.Length - (expBits + 1))

            Dim length As Integer = Len(matissa)
            Dim ps As Long = 2
            For x As Integer = 0 To length
                Dim temp As Integer = Val(Mid(matissa, x + 1, 1))

                If temp = 1 Then
                    dec += temp / ps
                End If
                ps *= 2
            Next

            dec = _sinal * 2 ^ (a - expAux) * dec

            Return dec
        End Function