我看你已经有了答案,但是...
private TextBox txtNumerator, txtDenominator, txtResult;
public MyClass() {
txtNumerator = new TextBox();
txtNumerator.TextChanged += new TextChangedEvent(TextBox_TextChanged);
txtDenominator = new TextBox();
txtResult = new TextBox();
}
private void TextBox_TextChanged(object sender, EventArgs e) {
double numerator = Convert.ToDouble(txtNumerator.Text.Trim());
double denominator = Convert.ToDouble(txtDemominator.Text.Trim());
if (denominator != 0) {
double result = numerator / denominator;
// Ref: http://www.csharp-examples.net/string-format-double/
txtResult.Text = string.Format("{0:0.00}", result);
} else {
throw new DivideByZeroException("Denominator can not be zero.");
}
}
您特别询问如何将双精度位到小数点后两位。String.Format 将为您做到这一点。
此外,使用事件处理程序,您的人员永远不必单击某些计算按钮。