实现效果:
关键知识:
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 //}