Mar*_*rth 0 c# visual-studio-2010 winforms
我有一个用C#编写的Windows窗体应用程序.
我正在寻找一种方法来验证我的价格textBox,以便它只接受双重格式的价格,例如允许0.01和1200.00,但在用户输入字符时提供错误.
我会除了看起来类似的代码
String price = tbx_price.Text.Trim();
if price is not a number
{
error message
}
else{
...
Run Code Online (Sandbox Code Playgroud)
我可以用什么方法来检查价格字符串是否只包含数字?请注意,我要求用户能够使用小数位,所以'.' 应该允许角色.
用途decimal.TryParse:
decimal d;
if (!decimal.TryParse(price, out d)){
//Error
}
Run Code Online (Sandbox Code Playgroud)
如果您还想验证价格(145.255无效):
if (!(decimal.TryParse(price, out d)
&& d >= 0
&& d * 100 == Math.Floor(d*100)){
//Error
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2293 次 |
| 最近记录: |