计算.net c#中的值 [英] Calculation of value in .net c#

查看:53
本文介绍了计算.net c#中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用c#

计算一个值但是一个符号乘法,除法,求和和减法是从数据库中检索的。

我遇到转换唱歌的问题。





我试试这段代码:



double aa;

aa = Convert.ToInt32(TextBox1.Text)+ Convert.ToInt32(TextBox2.Text)+ Convert.ToInt32(TextBox3.Text);



TextBox12.Text = aa.ToString();



其中textbox2是来自数据库的符号。


I want to calculate a value in c#
But a sign Multiplication ,division,summation and subtraction is retrieving from database.
I am getting a problem of converting of sing.


I try this code :

double aa;
aa = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox3.Text) ;

TextBox12.Text = aa.ToString ();

where textbox2 is sign coming from database.

推荐答案

你不能这样做。

如果你的运算符存储在textbox2中,你需要写一个开关案例基于此运算符的语句。

例如

You cannot do things this way.
If your operator is stored in textbox2, you need to write a switch case statement based on this operator.
E.g.
switch(TextBox2.Text)
case "+" : Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox3.Text) ; break;
case "*" : Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox3.Text) ; break;
case "/" : Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox3.Text) ; break;
case "-" : Convert.ToInt32(TextBox1.Text) - Convert.ToInt32(TextBox3.Text) ; break;


看看这里..如果你可以生成要作为字符串计算的表达式,请使用以下来评估。



Have a look here.. If you can generate the expression to be evaluated as a string, use following to evaluate.

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"]);
        }





快乐编码..

问候。



happy coding..
Regards.


使用开关或任何其他条件运算符来检查它是哪个符号然后相应地执行操作,即



Use switch or any other conditional operator for checking which sign it is then perform operation accordingly i.e.

double aa;
            switch (TextBox2.Text)
            {
                case "+":
                    aa = Convert.ToDouble(TextBox1.Text) + Convert.ToDouble(TextBox3.Text);
                    break;
                case "-":
                    aa = Convert.ToDouble(TextBox1.Text) - Convert.ToDouble(TextBox3.Text);
                    break;
                case "/":
                    aa = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(TextBox3.Text);
                    break;
                case "*":
                    aa = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox3.Text);
                    break;
            }


这篇关于计算.net c#中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆