1. 程式人生 > >對計數結果進行4舍5入

對計數結果進行4舍5入

實現效果:

關鍵知識:

Math.Round();

TryParse();

Math.Pow();

實現程式碼:

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             double num1, num2;
 4             if (double.TryParse(textBox1.Text, out num1) &&
 5                 double.TryParse(textBox2.Text, out
num2)) 6 { //得到四捨五入後的值 7 // textBox3.Text = round(num1+num2,comboBox1.SelectedIndex+1); 8 textBox3.Text = Math.Round(num1+num2,comboBox1.SelectedIndex+1).ToString(); 9 } 10 else { MessageBox.Show("輸入格式有誤,請檢查"); } 11 12
}//自定義四捨五入方法 13 //public string round(double d,int i) { 14 // return ((long)((long)(d * Math.Pow(10 ,(i+1)) + 5))/10 / Math.Pow(10 ,(i))).ToString(); 15 //}