简体   繁体   中英

how to take average of two text box values C#

I used this code but seems like it doesn't work. does any one have answer

    private void txtBfrLeft1_Leave(object sender, EventArgs e)
    {
        if (txtBfrRight1.Text != "" || txtBfrLeft1.Text != "")
        {
            int befrAvg1 = (Convert.(txtBfrRight1.Text) + Convert.ToDouble(txtBfrLeft1.Text)) / 2;
            txtBfrAvg1.Text = befrAvg1.ToString();
        }
    }       

Try this:

double befrAvg1 = (new []
{
    double.Parse(txtBfrRight1.Text),
    double.Parse(txtBfrLeft1.Text)
}).Average();

Or if you do actually want the result as an integer then this:

int befrAvg1 = (int)((new []
{
    double.Parse(txtBfrRight1.Text),
    double.Parse(txtBfrLeft1.Text)
}).Average());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM