public decimal DealWithNullValueWithDecimal(string txtBox)
{
if (string.IsNullOrEmpty(txtBox.Trim()))
{
return 0;
}
else
{
decimal dec = 0m;
try {
dec = Convert.ToDecimal(txtBox.Trim().ToString());
}
catch(Exception ex)
{
Alert("转换出错");
}
finally
{
}
return dec;
}
}
{
if (string.IsNullOrEmpty(txtBox.Trim()))
{
return 0;
}
else
{
decimal dec = 0m;
try {
dec = Convert.ToDecimal(txtBox.Trim().ToString());
}
catch(Exception ex)
{
Alert("转换出错");
}
finally
{
}
return dec;
}
}
本文介绍了一个实用的方法来处理文本框输入为空的情况,并安全地将字符串转换为decimal类型。当输入为空或无效时,该方法会返回默认值0,并在转换失败时给出提示。

被折叠的 条评论
为什么被折叠?



