使用Visual Basic将厘米转换为英里

时间:2013-11-23 13:17:44

标签: vb.net visual-studio-2012

我正在使用Visual Studio 2012和Visual Basic编程语言。我有一个目前转换长度的单位转换器。我的问题是,当我将1厘米转换为英里时,我得到6.214E-06。现在,我明白这是6.214 x 10 ^ -6,但我希望它显示实际数字而不是科学记数法。我相信对用户来说会容易得多。如何使其显示实际数字(0.000006214)?

            ' converts centimeter to...
            If cbo1.SelectedItem = "Centimeter" Then
                If cbo2.SelectedItem = "Kilometer" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 0.0001, 6).ToString.Trim
                ElseIf cbo2.SelectedItem = "Meter" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 0.01, 6).ToString.Trim
                ElseIf cbo2.SelectedItem = "Centimeter" Then
                    txtUnit2.Text = txtUnit1.Text
                ElseIf cbo2.SelectedItem = "Millimeter" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 10, 6).ToString.Trim
                ElseIf cbo2.SelectedItem = "Mile" Then
                    txtUnit2.Text = dblUnit1 * 0.000006214.ToString.Trim
                ElseIf cbo2.SelectedItem = "Yard" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 0.010936133, 6).ToString.Trim
                ElseIf cbo2.SelectedItem = "Foot" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 0.032808399, 6).ToString.Trim
                ElseIf cbo2.SelectedItem = "Inch" Then
                    txtUnit2.Text = Math.Round(dblUnit1 * 0.393700787, 6).ToString.Trim
                End If
            End If

4 个答案:

答案 0 :(得分:1)

查看standard numeric format strings

您需要的是使用“N”格式:

output = value.ToString("N")

这应输出数字为

  

0.000006214

答案 1 :(得分:1)

您可能正在寻找value.ToString("R") 见这里:http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

答案 2 :(得分:1)

您可以使用value.ToString("N99").TrimEnd("0"c)(99是格式说明符将使用的最大位数)。

或者你可以取数字的数字,并在前面加上适当数量的零,这适用于小于1E-99的数字,虽然我不认为这对用户来说会更容易:

Module Module1

    Function DoubleToString(x As Double) As String
        Dim sf As String = x.ToString()
        ' If the number would not be displayed in scientific format, there is no
        ' need to process it.
        Dim eLocation As Integer = sf.IndexOf({"E"c}, StringComparison.OrdinalIgnoreCase)
        If eLocation = -1 Then
            Return sf
        End If

        Dim decSeparator As Char = CChar(Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator)

        Dim magnitude As Integer = CInt(Math.Floor(Math.Log10(Math.Abs(x))))

        If magnitude < 0 Then
            Dim digits As String = (x * 10 ^ (-magnitude)).ToString().Replace(decSeparator, "")
            Dim sign As String = ""
            If x < 0 Then
                sign = "-"
                digits = digits.Substring(1)
            End If
            Return String.Format("{0}0{1}{2}{3}", sign, decSeparator, New String("0"c, -magnitude - 1), digits)
        Else
            ' Could process it to display large numbers.
            Return sf
        End If

    End Function
    Sub Main()
        Dim value As Double = 0.00006214
        ' A simple way.
        Console.WriteLine(value.ToString("N99").TrimEnd("0"c))

        Console.WriteLine(DoubleToString(value))

        Console.ReadLine()

    End Sub

End Module

DoubleToString(1.23456789E-190)给出了0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123456789

答案 3 :(得分:0)

将数据类型从Double切换到Decimal解决了这个问题!我只是为了找到更多限制或不需要的行为而与所有这些格式化方法搏斗。感谢所有人的投入。我的更新代码如下。

Private Function GetLength1(ByVal decLengthUnit1 As Decimal) As Decimal

    Dim decResult1 As Decimal

    If cboUnitType.SelectedItem = "Length" Then

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

    Return decResult1
End Function


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

    If suppressTextBox1TextChanged = False Then

        Decimal.TryParse(txtUnit1.Text, decUnit1)

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

End Sub