1

你们能帮忙解决这个问题吗?我需要将存储为 (xy) 的实数转换为 Visual Basic 中的十进制值,反之亦然。

该过程的任何伪代码/代码/答案都会很棒,干杯!马丁

4

2 回答 2

2
Dim realNumber = 123.456
Dim decimalValue = Convert.ToDecimal(realNumber)
realNumber = Decimal.ToDouble(decimalValue)
于 2012-07-02T22:25:32.887 回答
-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
于 2018-12-12T22:44:26.850 回答