下面的代码只允许输入整数,但不是只输入整数,我需要为用户提供输入双精度 (0.00) 的选项。有什么建议或修改吗?
if (_txStaticPressureUpdate.Trim() == "")
{
txSystemMessage.Text = "Field 'Static Pressure' is empty. Please enter a valid value.";
txSystemMessage.ForeColor = System.Drawing.Color.Red;
return;
}
//check that record info entered consists of numbers only / no special characters or letters
for (int j = 0; j < _txStaticPressureUpdate.Length; j++)
{
if (!char.IsControl(_txStaticPressureUpdate[j]))
{
txSystemMessage.Text = "Field 'Static Pressure' has an invalid value.";
txSystemMessage.ForeColor = System.Drawing.Color.Red;
return;
}
else
{
staticPressure = double.Parse(txStaticPressureUpdate.Text.Trim());
}
}
我在 ASPX/C# 网站上工作。许多可用选项适用于 WinForms。它仍然有效,但为了在公式的最终结果中更精确,我希望使用双打。
double.TryParse 可用于检查输入的字符串是否为双精度值。它返回一个布尔值,指示解析是否成功,并将解析的值传递给一个变量。
例如。
if (!double.TryParse(_txStaticPressureUpdate.Text.Trim(), out staticPressure))
{
txSystemMessage.Text = "Field 'Static Pressure' has an invalid value.";
txSystemMessage.ForeColor = System.Drawing.Color.Red;
return;
}
您也不需要遍历并检查单个字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句