c# - 如何用 double 和 int 划分两个 TextBox?
问题描述
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();
}
}
推荐阅读
- windows-services - 在 CI 服务器上运行时,testcafe E2E 测试在 Internet Explorer 11 上挂起(由 Windows 服务运行)
- python - 导出 svg 文件时,Plotly 无法访问 orca 服务器
- octobercms - 关系经理如何使用转发器字段?
- php - PHP在页面之间丢失会话
- c++ - 为什么 gcc 编译这个而 msvc 不编译
- c++ - 链表的通用函数 remove() 与成员函数 remove()
- java - 生成正则表达式以匹配字符串
- php - 用包含+符号的长字符串(超过1000个字符)在php中解码json字符串
- if-statement - Freemarker 如果条件为 NULL
- python - 熊猫数据框中每一列的唯一值及其计数