0

I am using Visual Studio 2012 and Visual Basic. My unit converter gives me an answer in scientific notation instead of the actual number that I am looking for. This occurs during the centimeter to mile conversion due to its multiple places past the decimal point. How can I format the output to give me the actual number instead of the notation? Currently, it is giving me this result: 1 centimeter = 6.214E-06 mile. I want this result: 1 centimeter = 0.000006214 mile. Thanks in advance, code is below.

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
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Zack
  • 117
  • 1
  • 8
  • 14

5 Answers5

0

try

Return CType(dblResult1.ToString("R"),Double)

ref: http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx (as noted above)

Rob
  • 3,488
  • 3
  • 32
  • 27
0

Change this:

txtUnit2.Text = GetLength1(dblUnit1)

to this:

txtUnit2.Text = Format(GetLength1(dblUnit1), "0.#########")
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
0

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

user3029534
  • 31
  • 1
  • 4
0

I resolved the entire issue by changing every Double data type to Decimal data type. Thanks for all the input anyway.

Zack
  • 117
  • 1
  • 8
  • 14
0

Based on my accepted answer to this related question ( https://stackoverflow.com/a/16091580/380384) you can try this code (ported to VB.BET from C#) that uses the common SI prefixes for formatting large numbers.

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

And test code

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

with results like:

    10.472m
    125.66m
    1.5080
    18.096
    217.15
    2.6058k
    31.269k
    375.23k
    4.5028M
    54.033M
Community
  • 1
  • 1
John Alexiou
  • 28,472
  • 11
  • 77
  • 133