[英]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) { }
}
不确定这是否是您所追求的,但它应该为您提供语法。
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();
}
}
如果您需要在除法之前将 costot 和 unidadesp 都更改为整数,请执行此操作。
res = (int)(Math.Round(Math.Round(costot) / Math.Round(unidadesp)));
再一次编辑
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.