使用Visual Basic的科学记数法问题

时间:2013-11-24 19:20:12

标签: vb.net

我正在使用Visual Studio 2012和Visual Basic。我的单位转换器给出了科学记数法的答案,而不是我想要的实际数字。这是在厘米到英里转换期间发生的,因为它有多个小数点后的位置。如何格式化输出以给出实际数字而不是符号?目前,它给我这个结果:1厘米= 6.214E-06英里。我想要这个结果:1厘米= 0.000006214英里。提前致谢,代码如下。

Private Function GetLength1(ByVal dblLengthUnit1 As Double) As Double

    Dim dblResult1 As Double

    If cboUnitType.SelectedItem = "Length" Then

        ' converts centimeter to...
        If cbo1.SelectedItem = "Centimeter" Then
            If cbo2.SelectedItem = "Kilometer" Then
                dblResult1 = (dblLengthUnit1 * 0.0001)
            ElseIf cbo2.SelectedItem = "Meter" Then
                dblResult1 = (dblLengthUnit1 * 0.01)
            ElseIf cbo2.SelectedItem = "Centimeter" Then
                dblResult1 = txtUnit1.Text
            ElseIf cbo2.SelectedItem = "Millimeter" Then
                dblResult1 = (dblLengthUnit1 * 10)
            ElseIf cbo2.SelectedItem = "Mile" Then
                dblResult1 = (dblLengthUnit1 * 0.000006214)
            ElseIf cbo2.SelectedItem = "Yard" Then
                dblResult1 = (dblLengthUnit1 * 0.010936133)
            ElseIf cbo2.SelectedItem = "Foot" Then
                dblResult1 = (dblLengthUnit1 * 0.032808399)
            ElseIf cbo2.SelectedItem = "Inch" Then
                dblResult1 = (dblLengthUnit1 * 0.393700787)
            End If
        End If

    Return dblResult1.ToString.Trim
End Function


Private Sub txtUnit1_TextChanged(sender As Object, e As EventArgs) Handles txtUnit1.TextChanged

    If suppressTextBox1TextChanged = False Then

        Double.TryParse(txtUnit1.Text, dblUnit1)

        ' if String.Empty
        If txtUnit1.Text = "" Then
            txtUnit2.Text = ""
        Else
            ' trigger the function
            suppressTextBox2TextChanged = True
            txtUnit2.Text = GetLength1(dblUnit1)
            suppressTextBox2TextChanged = False
        End If
    End If

End Sub

5 个答案:

答案 0 :(得分:0)

返回CType(dblResult1.ToString(“R”),Double)

参考:http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx(如上所述)

答案 1 :(得分:0)

改变这个:

txtUnit2.Text = GetLength1(dblUnit1)

到此:

txtUnit2.Text = Format(GetLength1(dblUnit1), "0.#########")

答案 2 :(得分:0)

txtUnit2.Text =格式(GetLength1(dblUnit1),“+ 0。#########”)

答案 3 :(得分:0)

我通过将每个Double数据类型更改为Decimal数据类型来解决整个问题。无论如何,感谢所有的输入。

答案 4 :(得分:0)

根据我对此相关问题(https://stackoverflow.com/a/16091580/380384)的接受答案,您可以尝试使用公共SI前缀格式化大数字的代码(从VB.BET移植到C#)。

Module DoubleEx
    Dim prefixes As String() = {"f", "a", "p", "n", "μ", "m", _
        String.Empty, "k", "M", "G", "T", "P", _
        "E"}

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Nice(x As Double, significant_digits As Integer) As String
        'Check for special numbers and non-numbers
        If Double.IsInfinity(x) OrElse Double.IsNaN(x) OrElse x = 0 Then
            Return x.ToString()
        End If
        ' extract sign so we deal with positive numbers only
        Dim sign As Integer = Math.Sign(x)
        x = Math.Abs(x)
        ' get scientific exponent, 10^3, 10^6, ...
        Dim sci As Integer = If(x = 0, 0, CInt(Math.Floor(Math.Log(x, 10) / 3)) * 3)
        ' scale number to exponent found
        x = x * Math.Pow(10, -sci)
        ' find number of digits to the left of the decimal
        Dim dg As Integer = If(x = 0, 0, CInt(Math.Floor(Math.Log(x, 10))) + 1)
        ' adjust decimals to display
        Dim decimals As Integer = Math.Min(significant_digits - dg, 15)
        ' format for the decimals
        Dim fmt As New String("0"c, decimals)
        If sci = 0 Then
            'no exponent
            Return String.Format((Convert.ToString("{0}{1:0.") & fmt) + "}", If(sign < 0, "-", String.Empty), Math.Round(x, decimals))
        End If
        ' find index for prefix. every 3 of sci is a new index
        Dim index As Integer = sci / 3 + 6
        If index >= 0 AndAlso index < prefixes.Length Then
            ' with prefix
            Return String.Format((Convert.ToString("{0}{1:0.") & fmt) + "}{2}", If(sign < 0, "-", String.Empty), Math.Round(x, decimals), prefixes(index))
        End If
        ' with 10^exp format
        Return String.Format((Convert.ToString("{0}{1:0.") & fmt) + "}·10^{2}", If(sign < 0, "-", String.Empty), Math.Round(x, decimals), sci)
    End Function

End Module

测试代码

Sub Main()
    Dim x = 12 * Math.PI / 3600
    For index = 1 To 10
        'Show 5 significant digits
        Debug.Print(x.Nice(5))
        x *=12
    Next
End Sub

结果如:

    10.472m
    125.66m
    1.5080
    18.096
    217.15
    2.6058k
    31.269k
    375.23k
    4.5028M
    54.033M