【问题标题】:TextBox not calculating the right wayTextBox 没有以正确的方式计算
【发布时间】:2015-08-28 11:39:11
【问题描述】:

我有一个 Windows 应用程序链接到一个数据库。当这个文本框的文本发生变化时,它应该从文本框中计算一些值。我写了,它工作正常,所有的,除了一个。如果 textBox2 中的文本小于 20,则计算正确,如果大于,则不再正确计算,我什至可以知道为什么会这样。自从我试图解决它以来已经有 2 天了,但什么也没有。任何人都可以得到它吗?

private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            int total = textBox1.Text.Length == 0 ? 0 : int.Parse(textBox1.Text);
            textBox2.Text = total.ToString();
            textBox7.Text = (total * 8).ToString();
        }

        private void textBox7_TextChanged_1(object sender, EventArgs e)
        {
            int ore_l = textBox7.Text == "" ? 0 : int.Parse(textBox7.Text);
            int ore_n = textBox8.Text == "" ? 0 : int.Parse(textBox8.Text);
            int t = ((ore_l - ore_n) / 8);
            textBox2.Text = t.ToString();
        }

        private void textBox8_TextChanged_1(object sender, EventArgs e)
        {
            //it is correct
            int ore_l = textBox7.Text == "" ? 0 : int.Parse(textBox7.Text);
            int ore_n = textBox8.Text == "" ? 0 : int.Parse(textBox8.Text);
            int t = ((ore_l - ore_n) / 8);
            textBox2.Text = t.ToString();

            //it isn't correct anymore
            int ac = label14.Text.Trim() == "" ? 0 : int.Parse(label14.Text);
            int zi_l = textBox1.Text.Trim() == "" ? 0 : int.Parse(textBox1.Text);
            int zi_luc = textBox2.Text.Trim() == "" ? 0 : int.Parse(textBox2.Text);
            int total = ac / zi_l * zi_luc;
            textBox6.Text = total.ToString();
        }

        private void textBox6_TextChanged_1(object sender, EventArgs e)
        {
            int s = textBox4.Text == "" ? 0 : int.Parse(textBox4.Text);
            int ac = label14.Text == "" ? 0 : int.Parse(label14.Text);
            textBox5.Text = (ac + s).ToString();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            int b = label21.Text == "" ? 0 : int.Parse(label21.Text);
            int sa = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
            int t = (b / sa + (75 / 100 * b / sa));
            textBox10.Text = t.ToString();


            int s = textBox4.Text == "" ? 0 : int.Parse(textBox4.Text);
            int ac = label14.Text == "" ? 0 : int.Parse(label14.Text);
            textBox5.Text = (ac + s).ToString();
        }

用这个值测试它: ac = 1400 ; zi_l = 23 ; zi_luc = 23。所以:1400/23*23。应该是 1400,因为它是 int,实际上显示的是 1380。

P.S:由于文本更改并且值错误,因此不会发生这种情况。我分别尝试了一个按钮和方法onClick,结果是一样的。谢谢!

【问题讨论】:

    标签: c# textbox calculator


    【解决方案1】:

    1400 除以 23 得到 60.8xxxxxxxxxxx。

    因为 1400 是整数,所以小数点后的数字被截去。计算结果为 60 * 23 = 1380。

    如果您想要更好的计算,您需要使用浮点数,然后使用 Math.RoundMidPointRoundingAwayFromZero

    【讨论】:

    • 你说的完全正确。找我2天。我太愚蠢了,竟然不去想这个。我的天啊。哈哈哈。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多