首页 > 解决方案 > 如何检查 TextBox.Text 是否为整数?

问题描述

我需要检查我的 TextBox 是否包含整数,即使是这种格式:“5.00”

我尝试了 Integer.TryParse(),但它返回 False。它仅在这种情况下起作用,当 TextBox 包含以下格式的数字时:“5”

标签: c#.netvb.net

解决方案


将您的字符串编号转换为Double 然后使用Math.Round()方法检查

//Convert to Double && round double number and check with original double number
if((Double.TryParse(textValue, out double number)) && Math.Round(number) == number)
{
 //Here you go;
}

.NET 小提琴


推荐阅读