With this code I can modify decimal place, using numericUpDown. This code works if I initialize myDecimal variable. But I need modify decimal place to a value typed in textbox.
In other word myDecimal = tbxConvertito.Text. But in this case, code not work.
Check screenshot in this page please: change decimal place in textbox using numericUpDown
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;
}
}
You can split property
TextfromtbxConvertito.Also when text will be changed in TextBox you must invoke
numericUpDown1_ValueChangedto restrict settings in NumericUpDown.Without lose data:
Scenarios:
if
you change value in NumericUpDown to 0 displayed value will be [1],
[1234], [12345]
if you change value in NumericUpDown to 1 again
displayed value will be [1,1], [1234], [12345,3]
Now we don’t lose any data. We update our original data only if user will type something in our textbox.