**需要添加两个文本框值 [英] ** Need to add two textbox values
本文介绍了**需要添加两个文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个文本框;
txtbox1 value =1/4;
txtbox2 value =1;
i想要使用c#代码添加这两个值..
i've 2 text box;
txtbox1 value ="1/4";
txtbox2 value ="1";
i want to add these two values using c# code behind..
推荐答案
你可以创建一个函数名为Evaluate并使用该函数来评估你的字符串(1/4)。
参见这个 [ ^ ]:
You can create a function called Evaluate and use that function to evaluate your string(1/4).
See this[^]:
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:
double value = Evaluate("1/4");
Console.WriteLine(value);//This will print 0.25
--Amit
--Amit
使用 - [MSDN] Decimal.Add方法 [ ^ ]。
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试过这个工作正常..
我们可以用其他任何方式解决这个问题吗?
i tried this this is working fine..
can we solve this in any other way?
这篇关于**需要添加两个文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文