Search code examples
c#winformsdecimalnumericupdown

change decimal place in textbox using numericUpDown


Im using Visual Studio 2010, windows form (c#).

I need change decimals place of value in textbox using numericUpDown1.

example:

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

I tryed this code:

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();
        }

    }

but not work. How can I solve this, please?


Solution

  • You should create field with decimal value, to keep original value.

    If you do that:

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

    You lose original value of your decimal variable.

    Try this:

    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();
            }
    }
    

    Solution without use Round method:

      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;
            }
        }