Vincenzo Lo Palo
Vincenzo Lo Palo

Reputation: 1381

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?

Upvotes: 0

Views: 2063

Answers (2)

kmatyaszek
kmatyaszek

Reputation: 19296

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

Upvotes: 2

Yakup &#220;nyılmaz
Yakup &#220;nyılmaz

Reputation: 404

You can achieve this using String operations. Try this one

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

And use it like this in your 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);
        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,",");
    }

}

Upvotes: 0

Related Questions