将2个文本框值相乘并显示在c#的第3个文本框中

问题描述:

您好我想要多个两位十进制数并在第三个文本框中显示结果或者标签。文字

通知:我的两个十进制数是我的数据库sql server的两个字段的数据,但是我的代码没有工作请帮帮我。和字段类型是十进制的

我的代码:



Hi I want to multiple two decimal number and display result in 3rd textbox or label.text
notice: my two decimal number are data of two field of my database sql server but my code does not work please help me. and type of field is decimal
my code :

protected void Button1_Click1(object sender, EventArgs e)
    {

        Decimal a, b;

        bool isAValid = Decimal.TryParse(TextBox1.Text, out a);
        bool isBValid = Decimal.TryParse(TextBox2.Text, out b);
        if (isAValid && isBValid)
                Label2.Text = (a * b).ToString();

            else
            Label2.Text = "Invalid input";
        }

    }





是否需要更改字段类型或更改我的代码?



Is Necessary to chang type of field or change my code?

rezaeti写道:
rezaeti wrote:

需要改变字段类型或更改我的代码?

Is Necessary to chang type of field or change my code?





是的,你真的需要更改你的代码!



取决于本地化(国家,地区设置)十进制和/或千位分隔符可能不同。因此,您必须使用 Decimal.TryParse(String,NumberStyles,IFormatProvider,十进制) [ ^ ]方法,使用特定的 CultureInfo类 [ ^ ]。



Yes, you do really have to change your code!

Depending on localization (country, regional settings) the decimal and/or thousand separator may differ. So, you have to use Decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal)[^] method, which provides proper "convertion" using specific CultureInfo class[^].