【发布时间】:2015-02-24 15:23:05
【问题描述】:
Basically, ive created a form that I can select different shapes that when a value on the track bar is selected, works out both the area and boundary length of either a circle, triangle or square.
这些值目前有很多小数位,我想设置单选按钮来选择小数点后 2、3 或 4 位。
private void sliderBar(object sender, EventArgs e)
{
textBox3.Text = trackBar1.Value.ToString();
if(circleButton.Checked == true)
{
textBox2.Text = (circle.getArea(trackBar1.Value)).ToString();
textBox1.Text = (circle.getBoundLength(trackBar1.Value)).ToString();
}
else if(squareButton.Checked == true)
{
textBox2.Text = (square.getArea(trackBar1.Value)).ToString();
textBox1.Text = (square.getBoundLength(trackBar1.Value)).ToString();
}
else
{
textBox2.Text = (triangle.getArea(trackBar1.Value)).ToString();
textBox1.Text = (triangle.getBoundLength(trackBar1.Value)).ToString();
}
if (decimalPlaces2Button.Checked == true)
{
TextBox2.Text = decimal.Round(textBox2, 2, MidpointRounding.AwayFromZero).ToDouble();
}
}
【问题讨论】:
-
Decimal.ToDouble() 可以带参数。方法请看 MSDN 文章。
-
Convert.ToDecimal(value)
-
有错别字,
TextBox2.Text = decimal.Round(textBox2, 2, MidpointRounding.AwayFromZero).ToDouble();应该是TextBox2.Text = decimal.Round(textBox2.Text /* HERE */, 2, MidpointRounding.AwayFromZero).ToDouble(); -
@CallumLinington 给出错误:'decimal.Round(decimal, int, System.MidpointRounding)' 的最佳重载方法匹配有一些无效参数
-
是的,所以你需要用
Double.Parse(textBox2.Text)包围textBox2.Text