none
VB.NET - Math operation with strings RRS feed

  • Question

  • Hi everyone!

    I have a string which comes into my form from a serial port like this: +0.21620000E+01.

    I need to solve it in this way: 0.2162*(10^1) '(Exponent can be different)

    How can I achieve it? Is it right to record result as 'Real' in my database?

    Thank you.

    Friday, May 24, 2019 10:37 AM

All replies

  • I solved and, to be fair, here is my code:

    Dim st3 As String
                    sub = TextBox.Text
                    Dim substring() As String
                    st3string = st3.Split(New Char() {"E"c})
                    TextBox.Text = substring(0)
                    TextBox.Text = extBox.Text.Replace(".", ",")
                    TextBox.Text = TextBox.Text * (10 ^ substring(1))

    It's split by two substrings I called substring which are used in different time.

    Friday, May 24, 2019 11:12 AM
  • I solved and, to be fair, here is my code:

    Dim st3 As String
                    sub = TextBox.Text
                    Dim substring() As String
                    st3string = st3.Split(New Char() {"E"c})
                    TextBox.Text = substring(0)
                    TextBox.Text = extBox.Text.Replace(".", ",")
                    TextBox.Text = TextBox.Text * (10 ^ substring(1))

    It's split by two substrings I called substring which are used in different time.

    Hi

    Very confusing.

    Are you suggesting that you intend to do math operations using strings instead of numeric values?

    Based on the information you supplied, here are some illustrations of what can be done with strings (display and parsing)

    Dim str As String = "+0.21620000E+01"
    Dim value As Double = 0
    Double.TryParse(str, value)
    ' value = 2.162
    
    Dim reformattedStr As String = value.ToString("E3")
    ' reformattedStr = 2.162E+000
    
    Double.TryParse(reformattedStr, value)
    ' value = 2.162
    
    
    ' get the display style you define in
    ' your 'answer'
    Dim s2 As String = str.Replace("E", "*(10^").Replace("+", Nothing) & ")"
    ' s2 = "0.21620000*(10^01)"
    
    
    Double.TryParse(s2, value)
    ' value = 0 (couldn't parse 
    ' that String) so useless for
    ' math use

    To do math operations, you should use numeric values and only display the values in what ever way you want.


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, May 24, 2019 2:53 PM
    Friday, May 24, 2019 2:51 PM
  • I solved and, to be fair, here is my code:

    Dim st3 As String
                    sub = TextBox.Text
                    Dim substring() As String
                    st3string = st3.Split(New Char() {"E"c})
                    TextBox.Text = substring(0)
                    TextBox.Text = extBox.Text.Replace(".", ",")
                    TextBox.Text = TextBox.Text * (10 ^ substring(1))

    It's split by two substrings I called substring which are used in different time.

    Hi,

    Is your problem solved? If so, please mark your solution as answers.  

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 27, 2019 8:24 AM
  •  

     Dim x = CDbl("+0.21620000E+01")


    Success
    Cor

    Monday, May 27, 2019 2:18 PM