Hi paulrajmca,
Use the below line to calculate like mathematical formula.
string output = new System.Data.DataTable().Compute(txtCalculate.Text.Trim(), null).ToString();
If you want to simply calculate like calculator then use the below.
C#
string[] decimalNumbers = txtCalculate.Text.Split('+', '-', '*', '/');
string[] operators = txtCalculate.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;
}
}
}
lblResult.Text = Math.Round(intSum, 2).ToString();