Decimal places in the NumericUpDown control

I apologize if the answer was given earlier. I searched and did not find a suitable answer.

I have a numericupdowncontrol that accepts user input. Right now, I have set the decimalPlaces property to 2. If the user logs in to 1.23, he remains true. However, if the user enters 1.2, it displays 1.20. This is not what I want. It should display 1.2, not 1.20. Is there any way to do this? If the user enters 1, then it should be 1, not 1.00. How to do it?

Thanks a lot!

+4
source share
1 answer

If you don't mind NumericUpDown up your NumericUpDown , you can do it very simply, shortly and reliably:

 //You can use this class instead of the standard NumericUpDown public class CustomNumericUpDown : NumericUpDown { //Override this to format the displayed text protected override void UpdateEditText() { Text = Value.ToString("0." + new string('#', DecimalPlaces)); } } 
+4
source

Source: https://habr.com/ru/post/1500583/


All Articles