My form contains several NumericUpDown controls. These controls show different amount of decimal places.
Later in my code I put the different NumericUpDown.Value‘s in a string array arrStr() like so:
arrStr(1) = NumericUpDown1.Value
arrStr(2) = NumericUpDown2.Value
arrStr(3) = NumericUpDown3.Value
Then I print the array with the File.WriteAllLines function to a text file.
If for example NumericUpDown1.Value = 1.00, NumericUpDown2.Value = 2.30 and NumericUpDown3.Value = 2.124 the file has the following values in it:
1
2.3
2.124
I would like to see:
1.00
2.30
2.124
I have tried Format which works, but that formatting method is not convenient as the amount of decimal places is already set for each NumericUpDown. It would be annoying to do the work again, but now with Format.
You could use
String.Formatto force two decimal places:Standard Numeric Format Strings
Edit: If you’re array is large and you want to avoid:
arrStr(1) = String.Format("{0:f2}", NumericUpDown1.Value)up toarrStr(86) = String.Format("{0:f2}", NumericUpDown86.Value)You can use LINQ to create the array. Assuming that your
NumericUpDowncontrols are all in aGroupBoxcalledNumericGroupBox. You can “inject” the correct number of decimal places:and here’s a version that searches all
TabPagesof yourTabControl(as commented):