以十进制字符串格式在运行时检索两个大功率的最快方法是什么?

时间:2017-09-10 15:52:03

标签: vb.net

为了显示大数字,我需要能够以十进制字符串格式检索两个幂。以下代码是我能做的最好的,有更快的方法吗?

Public NotInheritable Class TwoPowerOf

    Private DecimalString As String = ""

    Public Sub New(ByVal MyInteger As Integer)
        If MyInteger > -256 And MyInteger < 1024 Then
            Me.DecimalString = GetTwoPowerOf(MyInteger)
        End If
    End Sub

    Public Overrides Function ToString() As String
        Return Me.DecimalString
    End Function

    Protected Function GetTwoPowerOf(ByVal MyInteger As Integer) As String
        Dim result As String = ""
        Dim d As New Dictionary(Of Integer, String)
        '...
        d.Add(2, "4")
        d.Add(1, "2")
        d.Add(0, "1")
        d.Add(-1, "0.5")
        d.Add(-2, "0.25")
        d.Add(-3, "0.125")
        d.Add(-4, "0.0625")
        d.Add(-5, "0.03125")
        d.Add(-6, "0.015625")
        d.Add(-7, "0.0078125")
        d.Add(-8, "0.00390625")
        ' ...
        result = d.Item(MyInteger)
        Return result
    End Function
End Class

撤退就是这样的:

Sub whatever()
    Dim MyInteger as Integer
    Dim MyDecimalString as String = New TwoPowerOf(MyInteger).ToString
    Console.WriteLine(MyDecimalString)
End Sub

或者,有没有办法将大二进制数转换为小数而不使用2的幂?

0 个答案:

没有答案