Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i've 2 text box;

txtbox1 value ="1/4";
txtbox2 value ="1";

i want to add these two values using c# code behind..
Posted
Comments
Richard MacCutchan 28-Jun-13 3:54am    
What have you tried and where are you stuck?

You can create a function called Evaluate and use that function to evaluate your string(1/4).
See this[^]:
C#
static double Evaluate(string expression)
{
    var loDataTable = new DataTable();
    var loDataColumn = new DataColumn("Eval", typeof(double), expression);
    loDataTable.Columns.Add(loDataColumn);
    loDataTable.Rows.Add(0);
    return (double)(loDataTable.Rows[0]["Eval"]);
}

Now call this function:
C#
double value = Evaluate("1/4");
Console.WriteLine(value);//This will print 0.25


--Amit
 
Share this answer
 
Comments
VIP Venkatesan 28-Jun-13 5:06am    
thank u..
_Amy 28-Jun-13 5:06am    
Welcome. :)
+5ed... :)
_Amy 28-Jun-13 5:44am    
Thank you Tadit. :)
Welcome...
 
Share this answer
 
Comments
_Amy 28-Jun-13 5:09am    
I don't think the function will be helpful for OP's scenario. He is trying to evaluate a string which contains some mathematical operations. Please see my Solution[^].
Oh yes.... You are correct. I just saw the Add function and added that here.
Thanks for indicating... :)
  string a = "1/4";
  string b = "1";
  string val1;
  string val2;

string[] w=a.Split('/');

 val1 = (Convert.ToDecimal(w[0]) / Convert.ToDecimal(w[1])).ToString();
 val2 = Convert.ToDecimal(b).ToString();
 string total = (Convert.ToDecimal(val1) + Convert.ToDecimal(val2)).ToString();


i tried this this is working fine..

can we solve this in any other way?
 
Share this answer
 
v2
Comments
_Amy 28-Jun-13 4:47am    
it's not a proper way to do this. See Solution 4[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900