locked
How to add Decimal out of a textblock RRS feed

  • Question

  • Hello everyone,

    How do I add decimal number (example 123.54) listed in a textblock to be added to another number from a textblock (example 25)? When I use the code below I get errors. I am still learning C#. Below is the code I am using when a button action is clicked:

     private void Button_Click_1(object sender, RoutedEventArgs e)
            {
               
    
                OutputText.Text = Convert.ToString(Convert.ToInt32(Input1.Text) +
                    Convert.ToInt32(Input2.Text));
          
            }    

    Thanks for the help,

    John


    John Lisenby developer of My PC Playbook

    Tuesday, October 22, 2013 6:32 PM

Answers

  • Try this, we can use Int.TryParse if second input is for sure int. 

     double inputOneValue = 0.0, inputTwoValue = 0.0;
                if (true == Double.TryParse(Input1.Text, out inputOneValue) && true == Double.TryParse(Input2.Text, out inputTwoValue))
                {
                    OutputText.Text = (inputOneValue + inputTwoValue).ToString();
                }


    Thanks, Sachin


    • Edited by Sachin S Tuesday, October 22, 2013 6:54 PM
    • Marked as answer by John Lisenby Jr Wednesday, October 23, 2013 2:43 AM
    Tuesday, October 22, 2013 6:53 PM

All replies

  • Try this, we can use Int.TryParse if second input is for sure int. 

     double inputOneValue = 0.0, inputTwoValue = 0.0;
                if (true == Double.TryParse(Input1.Text, out inputOneValue) && true == Double.TryParse(Input2.Text, out inputTwoValue))
                {
                    OutputText.Text = (inputOneValue + inputTwoValue).ToString();
                }


    Thanks, Sachin


    • Edited by Sachin S Tuesday, October 22, 2013 6:54 PM
    • Marked as answer by John Lisenby Jr Wednesday, October 23, 2013 2:43 AM
    Tuesday, October 22, 2013 6:53 PM
  • Thanks Sachin! It worked!

    John


    John Lisenby developer of My PC Playbook

    Wednesday, October 23, 2013 2:43 AM