2015-08-28 81 views
0

我有一個Windows應用程序鏈接到數據庫。當這個文本框的文本改變時,它應該從文本框中計算一些值。我寫道,它工作正常,除了一個之外,他們都是。如果textBox2中的文本不到20,它會正確計算,如果它在上面,它不再正確計算,我甚至可以知道爲什麼。自從我一直試圖解決這個問題以來,至今已有2天,但沒有任何結果。任何人都可以得到它?TextBox不計算正確的方式

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方法分別嘗試,結果是一樣的。謝謝 !

回答

2

將1400除以23得到60.8xxxxxxxxxxx。

因爲1400是一個整數,所以小數點後的數字被切掉。計算是60 * 23 = 1380.

如果您想要更好的計算,您需要使用浮點數,然後使用Math.RoundMidPointRoundingAwayFromZero

+0

你該死的恰到好處。給我2天。我愚蠢的是不要去想這件事。我的天啊。 HAHAH。非常感謝 ! – Ezekiel