Why does not String.Format work in a subtraction of textbox?

3

I have been trying to put thousands points in the heritage textbox for hours, which comes from a subtraction between less passive assets, but when it is subtracted when it is subtracted and it stops calculating, why does this happen?

protected void txtActivos_TextChanged(object sender, EventArgs e)
    {
        double calc1 = 0;
        double calc2 = 0;

        if (!String.IsNullOrEmpty(txtActivos.Text) || 

    !String.IsNullOrEmpty(txtPasivos.Text))
            {
                calc1 = Convert.ToDouble(txtActivos.Text);
                calc2 = Convert.ToDouble(txtPasivos.Text);
                var calcTotal = Convert.ToString(calc1 - calc2);
                txtPatrimonio.Text = String.Format("{0:C2}", calcTotal);
            }

        }

    
asked by Vulpex 24.04.2018 в 15:14
source

1 answer

3

String.Format works on certain things.

In your case, C2 implies a currency format value, but it is waiting for a number as a variable.

As what you're going through is a chain, it does not make any kind of transformation, because it does not know how to format that string, since you told it to format a number.

To solve this, what you have to do is:

calc1 = Convert.ToDouble(txtActivos.Text);
calc2 = Convert.ToDouble(txtPasivos.Text);
var calcTotal = calc1 - calc2;
txtPatrimonio.Text = String.Format("{0:C2}", calcTotal);

Since that way, calcTotal will be a number and format will be able to recognize it.

    
answered by 24.04.2018 / 16:00
source