【问题标题】:C# - How can I convert the string value to decimal places?C# - 如何将字符串值转换为小数位?
【发布时间】: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

标签: c# string format decimal


【解决方案1】:

这是一个有效的解决方案,它不会对您的数字进行四舍五入。

static double TakeDecimals(double value, int decimalCount)
    {
        var truncation = Math.Pow(10, decimalCount);
        return Math.Truncate(value * truncation) / truncation;
    }

像这样称呼

var input=24.343545;
TakeDecimals(input, 2);//24.34
TakeDecimals(input, 3);//24.343
TakeDecimals(input, 4);//24.3435

更新

在你的情况下,有一个字符串,你可以在调用方法之前做Convert.ToDouble(yourString)

【讨论】:

    【解决方案2】:

    您可以使用Math.Round(double, int32)

    Math.Round(value, 2);
    

    【讨论】:

    • so, textBox2.Text = Math.Round(value, 2); ?
    • 这个值已经是一个字符串了?它给出了一个错误,提示“当前上下文中不存在名称‘值’”
    • 你认为我应该投入什么而不是价值?
    • 您需要使用 Double.Parse() 解析 Textbox2.Text 值
    【解决方案3】:

    你可以用这个:

        decimal convertedValue;
        decimal.TryParse(textBox2.Text,out convertedValue);
       textBox2.Text = Math.Round(convertedValue, 2).ToString();
    

    【讨论】:

    • @Math.Round(convertedValue, 2);给我一个错误,说明无法将类型“十进制”隐式转换为“字符串”
    • 抱歉只使用 toString() : textBox2.Text = Math.Round(convertedValue,2).toString();
    • 这一切都在一行中吗?
    【解决方案4】:

    首先使用“Convert.ToDecimal”将字符串转换为十进制。然后使用“Math.Round”对小数进行四舍五入(2、3 或 4 位小数)。

    decimal area;   
    textBox3.Text = trackBar1.Value.ToString();
    
        if(circleButton.Checked == true)
        {
        area = circle.getArea(trackBar1.Value)
            textBox2.Text = area.ToString();
            textBox1.Text = (circle.getBoundLength(trackBar1.Value)).ToString();
        }
        else if(squareButton.Checked == true)
        {
        area = square.getArea(trackBar1.Value)
            textBox2.Text = area.ToString();
            textBox1.Text = (square.getBoundLength(trackBar1.Value)).ToString();
        }
        else
        {
        area = triangle.getArea(trackBar1.Value)
            textBox2.Text = area.ToString();
            textBox1.Text = (triangle.getBoundLength(trackBar1.Value)).ToString();
        }
    
        if (decimalPlaces2Button.Checked == true)
        {
        decimal number1 = Convert.ToDecimal(area);
            decimal numWithTwoDecimalPlace = Math.Round(number1, 2);
                TextBox2.Text = numWithTwoDecimalPlace.ToString();
        }
    else if (decimalPlaces3Button.Checked == true)
        {
        decimal number1 = Convert.ToDecimal(area);
            decimal numWithTwoDecimalPlace = Math.Round(number1, 3);
                TextBox2.Text = numWithTwoDecimalPlace.ToString();
        }
    

    【讨论】:

    • 嗨@Nat,你已经指定了一个字符串编号,但在我的程序中,这个数字会根据跟踪栏上标记的位置而改变
    • 十进制数1 = Convert.ToDecimal(textBox2.Text);
    • 使用上面的字符串值。
    • 它已经接受了代码但没有在表单的文本框中更新它
    • 尝试 {十进制数1 = Convert.ToDecimal(textBox2.Text);十进制 numWithTwoDecimalPlace = Math.Round(number1, 2); TextBox2.Text = numWithTwoDecimalPlace.ToString(); } catch (Exception ex) { throw ex; }
    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 2015-08-25
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多