Results 1 to 5 of 5

Thread: [RESOLVED] Calculating numbers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2010
    Posts
    130

    Resolved [RESOLVED] Calculating numbers

    In the TextBox1 I have the following numbers:5 4.1 6.23 0 0 7 7.924


    Possible to calculate all the numbers in this way?

    like replace all the "space" in "+" and get the answer or something?

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Calculating numbers

    I like your thinking.. maybe in the next version of VS

    For now though, I would split the values into an array. Then you can loop through the results and add them up.
    VB Code:
    1. Dim TextboxText As String = "5 4.1 6.23 0 0 7 7.924"
    2.  
    3. Dim ArrayOfValues() As String = TextboxText.Split(" ")
    4. Dim Sum As Double = 0
    5.  
    6. For Each Value As String In ArrayOfValues
    7.     If Double.TryParse(Value, Nothing) Then Sum += CDbl(Value)
    8. Next
    9.  
    10. MsgBox(Sum)

  3. #3
    Member
    Join Date
    Mar 2008
    Location
    East Kent, UK
    Posts
    34

    Re: Calculating numbers

    Quote Originally Posted by jay20aiii View Post
    I like your thinking.. maybe in the next version of VS
    It's actually possible in 2005, and may be possible in other versions as well :-

    Code:
            Dim TextBoxText As String = "5 4.1 6.23 0 0 7 7.924"
    
            Dim Formula as String = TextBoxText.Replace(" ", "+")
    
            Dim Evaluator As New DataTable
            Dim Result As Double = 0.0
            Result = Convert.ToDouble(Evaluator.Compute(Formula, ""))
    
            MessageBox.Show(Result.ToString)
    It will also cope with brackets, for example :-

    Code:
            Dim Formula As String = "5+(3*6)"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2010
    Posts
    130

    Re: Calculating numbers

    Quote Originally Posted by jay20aiii View Post
    I like your thinking.. maybe in the next version of VS

    For now though, I would split the values into an array. Then you can loop through the results and add them up.
    VB Code:
    1. Dim TextboxText As String = "5 4.1 6.23 0 0 7 7.924"
    2.  
    3. Dim ArrayOfValues() As String = TextboxText.Split(" ")
    4. Dim Sum As Double = 0
    5.  
    6. For Each Value As String In ArrayOfValues
    7.     If Double.TryParse(Value, Nothing) Then Sum += CDbl(Value)
    8. Next
    9.  
    10. MsgBox(Sum)

    Thank you very much

  5. #5
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: [RESOLVED] Calculating numbers

    Cool, I'm liking that DataTable function InetiaM. I bet I will be able to find a use for it at some point in the future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width