我有一个 TextBox,它应该只接受数字(它们可以是简单的 int
或 float
,以 E 作为功率因数)。
所以一些有效的例子是:
1
+1.1
0
.1
1E-1
1e-2
-1.66
-6e-10
无效示例:
E-1
通常它应该以数字或 + 和 - 符号或点开头。
然后我将它放在TextChanged
事件中就可以了。
textBox.Text = Regex.Replace(textBox.Text, @"[\d]", ""); //so useless!
最佳答案
如果你想使用你的指数,试试这个(一定要添加 using System.Globalization;
):
decimal value;
if(Decimal.TryParse(textBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture, out value))
{
textBox.Text = value.ToString();
}
关于c# - 正则表达式只允许数字字符加号、点和 E,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8726855/