2013-11-23 95 views
0

我正在使用Visual Studio 2012和Visual Basic編程語言。我有一個單位轉換器,目前正在轉換長度。我的問題是,當我轉換1釐米到英里我得到6.214E-06。現在,我明白這是6.214 x 10^-6,但我希望它顯示實際數字而不是科學記數法。我相信用戶會更容易。我如何讓它顯示實際的號碼(0.000006214)?使用Visual Basic將釐米轉換爲英寸

  ' 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 

回答

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 
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.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

相關問題