简体   繁体   中英

How to divide two TextBoxes with a double and a int?

private void button1_Click(object sender, EventArgs e)
{
    int res = 0;
    try
    {
        res = Convert.ToInt32(costot.Text) / Convert.ToInt32(unidadesp.Text);
        costou.Text = res.ToString();
    }
    catch (Exception ex) { }
}

Not sure if this is what you are after but it should give you the syntax.

    private void button1_Click(object sender, EventArgs e)
    {
        int res = 0;
        if (double.TryParse(costot.Text, out double costot) && double.TryParse(unidadesp.Text, out double unidadesp) && unidadesp != 0)
        {
            res = (int)(Math.Round(costot / unidadesp));
            costou.Text = res.ToString();
        }
    }

If you need the costot and unidadesp both changed to Integers before the division then do this.

res = (int)(Math.Round(Math.Round(costot) / Math.Round(unidadesp)));

One more edit

    private void button1_Click(object sender, EventArgs e)
    {

        if (double.TryParse(textBox1.Text, out double costot) && int.TryParse(textBox2.Text, out int unidadesp) && unidadesp != 0)
        {
           var res = costot / unidadesp;
            textBox3.Text = res.ToString();
        }
    }

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