C#后台判断一个字符串是否为整数或者带两位小数的数字
1./// <summary>
2./// 判断一个字符串是否为合法整数(不限制长度)
3./// </summary>
4./// <param name="s">字符串</param>
5./// <returns></returns>
6.public static bool IsInteger(string s)
7.{
8.string pattern = @"^\d*$";
9.return Regex.IsMatch(s,pattern);
10.}
11./**//// <summary>
12./// 判断一个字符串是否为合法数字(0-32整数)
13./// </summary>
14./// <param name="s">字符串</param>
15./// <returns></returns>
16.public static bool IsNumber(string s)
17.{
18.return IsNumber(s,32,0);
19.}
param name20./**//// <summary>
21./// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
22./// </summary>
23./// <param name="s">字符串</param>
24./// <param name="precision">整数位数</param>
25./// <param name="scale">小数位数</param>
26./// <returns></returns>
27.public static bool IsNumber(string s,int precision,int scale)
28.{
29.if((precision == 0)&&(scale == 0))
30.{
31.return false;
32.}
33.string pattern = @"(^\d{1,"+precision+"}";
34.if(scale>0)
35.{
36.pattern += @"\.\d{0,"+scale+"}$)|"+pattern;
37.}
38.pattern += "$)";
39.return Regex.IsMatch(s,pattern);
40.}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论