NumericUpDown控件中的小数位

时间:2013-09-04 17:03:37

标签: c# winforms

如果之前已经回答过,我很抱歉。我四处搜寻,找不到合适的答案。

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

非常感谢!

1 个答案:

答案 0 :(得分:4)

如果您不介意自定义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));
    }
}
相关问题