NumericUpDown控件中的小数位数



如果以前有人回答过这个问题,我很抱歉。我到处找,找不到合适的答案。

我有一个接受用户输入的numericupdown控件。现在我已经将decimalPlaces属性设置为2。如果用户输入1.23,它将保持正确。但是,如果用户输入1.2,则显示1.20。那不是我想要的。它应该显示1.2而不是1.20。有办法做到这一点吗?如果用户输入1,则应为1,而不是1.00。如何做到这一点?

非常感谢!

如果你不介意自定义NumericUpDown,你可以做到这一点,它非常简单、简短和可靠:

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

最新更新