2013-11-24 47 views
0

我正在使用Visual Studio 2012和Visual Basic。我的單位轉換器給我一個科學記數法的答案,而不是我正在尋找的實際數字。這發生在釐米到英里的轉換過程中,因爲它的多個地方超過了小數點。我如何格式化輸出以給我實際的數字而不是符號?目前,它給了我這個結果:1釐米= 6.214E-06英里。我想要這個結果:1釐米= 0.000006214英里。在此先感謝,代碼如下。使用Visual Basic的科學記數法問題

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 
+0

http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.110 %29.aspx –

+0

看看這個宏大的圖書館http://www.codeproject.com/Articles/611731/Working-with-Units-and-Amounts – ElektroStudios

回答

0

更改此:

txtUnit2.Text = GetLength1(dblUnit1) 

對此:

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

糟糕,固定錯誤... – RBarryYoung

0

txtUnit2.Text =格式(GetLength1(dblUnit1), 「+0。#########」)

0

我通過改變每一個Double數據類型爲十進制數據類型解決整個問題。無論如何感謝所有的輸入。

0

根據我對此相關問題的接受答案(https://stackoverflow.com/a/16091580/380384),您可以嘗試使用常用SI前綴格式化大數的此代碼(從C#移植到VB.BET)。

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