使用numericUpDown更改文本框中的小数位

时间:2013-01-19 12:02:51

标签: c# winforms decimal numericupdown

我正在使用Visual Studio 2010,Windows窗体(c#)。

我需要使用numericUpDown1在文本框中更改小数位值。

示例:

enter image description here enter image description here

enter image description here enter image description here

enter image description here enter image description here

enter image description here enter image description here

enter image description here enter image description here

我试过这段代码:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {

      //  tbxConvertito.Text = (decimal.Parse(tbxConvertito.Text) * 10m).ToString();

        if (numericUpDown1.Value == 0)
        {
            int decimalPlace = 0;
           // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

            decimal xDecimal = decimal.Parse(tbxConvertito.Text);
            tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();
        }

        if (numericUpDown1.Value == 1)
        {
            int decimalPlace = 1;
            // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

            decimal xDecimal = decimal.Parse(tbxConvertito.Text);
            tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();
        }

    }

但不行。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您应该创建带小数值的字段,以保留原始值。

如果你这样做:

int decimalPlace = 1;
decimal xDecimal = decimal.Parse(tbxConvertito.Text);
tbxConvertito.Text = (Math.Round(xDecimal, decimalPlace)).ToString();

您将丢失十进制变量的原始值。

试试这个:

public partial class Form1 : Form
{
        public decimal myDecimal = 3755.25012345M;

        public Form1()
        {
            InitializeComponent();

            tbxConvertito.Text = myDecimal.ToString();

            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {          
            int decimalPlace = (int)numericUpDown1.Value;
            tbxConvertito.Text = Decimal.Round(myDecimal, decimalPlace).ToString();
        }
}

不使用Round方法的解决方案:

  public partial class Form1 : Form
    {
        public decimal myDecimal = 3755.25012345M;

        public Form1()
        {
            InitializeComponent();

            tbxConvertito.Text = myDecimal.ToString();

            numericUpDown1_ValueChanged(this, EventArgs.Empty);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {          
            int decimalPlace = (int)numericUpDown1.Value;

            string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });

            string tmp = string.Empty;

            if (decimalPlace <= numbers[1].Length)
            {
                tmp = "," + numbers[1].Substring(0, decimalPlace);

                if (tmp.EndsWith(","))
                    tmp = string.Empty;
            }
            else
                tmp = "," + numbers[1];

            tbxConvertito.Text = numbers[0] + tmp;
        }
    }

答案 1 :(得分:0)

您可以使用String操作来实现此目的。试试这个

decimal xDecimal = decimal.Parse(tbxConvertito.Text);
string str = xDecimal.ToString();
if (!str.Contains(","))
            decimalPlace--;
tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");

在代码中使用它

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{

  //  tbxConvertito.Text = (decimal.Parse(tbxConvertito.Text) * 10m).ToString();

    if (numericUpDown1.Value == 0)
    {
        int decimalPlace = 0;
       // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

        decimal xDecimal = decimal.Parse(tbxConvertito.Text);
        string str = xDecimal.ToString();
        if (!str.Contains(","))
            decimalPlace--;
        tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");
    }

    if (numericUpDown1.Value == 1)
    {
        int decimalPlace = 1;
        // Decimal xDecimal = decimal.Parse(tbxConvertito.text);

        decimal xDecimal = decimal.Parse(tbxConvertito.Text);
        string str = xDecimal.ToString();
        if (!str.Contains(","))
            decimalPlace--;
        tbxConvertito.Text = str.Replace(",", "").Insert((str.Length-1) - decimalPlace,",");
    }

}