You must Sign In to post a response.
  • Category: ASP.NET

    How to Calculate Arithmetic operators in a single textbox in CSharp

    hi Developers

    How enter Multiple values in a single Textbox and how to Split it and How to Calculate it on both Running time and C#.

    i have a Textbox named txtDetails i should enter Many values or More than one value .
    i want to Split it by + or - or * and perform the calculation.
    if i entered values like this 10+20-10*2/5 the result will be =8.(like a simple calculator)
    i can give Decimals too.
    else
    if i entered values like this 20 the result will be =20. thats all

    this is my requirement still i have been trying with google. if anyone of know how to do this please guide me to i am done this task quickly.

    thanking you
    Paul.S
    Prayer throw, Impossible becomes Possible.
  • #768506
    i have done Splitting only if there is Arithmatic symbols.
    but i can able to only Addition(+) ,ex=10+10-10=30 but my expected output is 10.
    so how can i do calculations depending upon Arithmetic operator which is i am splitting.
    i am confused with this following code . if possible with my code na please suggest me

    protected void Button1_Click(object sender, EventArgs e)
    {
    string[] parts = txt2.Text.Split('+', '-', '*', '/');
    decimal intSum = 0;
    foreach (string item in parts)
    {
    intSum = intSum + Convert.ToDecimal(item);
    }
    txt4.Text = intSum.ToString();
    }

    thanking You
    Paul.S

  • #768509
    thanks to all

    Finally i got exact output with following Code

    protected void Button1_Click(object sender, EventArgs e)
    {
    string[] decimalNumbers = txt2.Text.Split('+', '-', '*', '/');
    string[] operators = txt2.Text.Split('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.');
    List<string> s = new List<string>();
    foreach (var item in operators)
    {
    if (!string.IsNullOrEmpty(item))
    {
    s.Add(item);
    }
    }
    decimal intSum = 0;
    for (int i = 0; i < decimalNumbers.Length; i++)
    {
    if (i == 0)
    {
    intSum = Convert.ToDecimal(decimalNumbers[i]);
    }
    if (i > 0)
    {
    string oprator = s[i - 1];
    switch (oprator)
    {
    case "+":
    intSum = intSum + Convert.ToDecimal(decimalNumbers[i]);
    break;
    case "-":
    intSum = intSum - Convert.ToDecimal(decimalNumbers[i]);
    break;
    case "*":
    intSum = intSum * Convert.ToDecimal(decimalNumbers[i]);
    break;
    case "/":
    intSum = intSum / Convert.ToDecimal(decimalNumbers[i]);
    break;
    }
    }
    }
    string total = Convert.ToString(intSum);
    decimal c = Convert.ToDecimal(total);
    total = c.ToString("0.00");
    txt4.Text = total;
    }

    Thanking You
    Paul.S

  • #768511
    Hi,

    Good to hear that you got the solution. But this was not a optimize solution.
    For every case you are adding a new Case to switch control which makes your application more dependency.

    Suppose in future you are getting a new operator like Modulo(%). So that time you again need to modify this function and change all the coding and logics.

    So try to use Delegate for this functionality to remove the dependency in your application.

    Thanks,
    Mani

  • #768513
    There are various ways to Calculate Arithmetic operators in a single textbox in CSharp. Here is one example for you
     {
    public partial class ArthOperations : Form
    {
    public ArthOperations()
    {
    InitializeComponent();
    textBox3.Enabled = false;
    }
    private void add(object sender, EventArgs e)
    {
    float first;
    float second;
    float output;
    first = Convert.ToInt32(textBox1.Text);
    second = Convert.ToInt32(textBox2.Text);
    output = first + second;
    textBox3.Text=(output.ToString());
    }

    private void sub(object sender, EventArgs e)
    {
    float first;
    float second;
    float output;
    first = Convert.ToInt32(textBox1.Text);
    second = Convert.ToInt32(textBox2.Text);
    output = first - second;
    textBox3.Text = (output.ToString());
    }
    private void clear(object sender, EventArgs e)
    {
    textBox1.Text = "";
    textBox2.Text = "";
    textBox3.Text = "";
    }
    }
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.