C#判断整数、小数位数的判定

  1. public static bool IsInteger(string s)
  2. {
  3.     string pattern = @"^\d*$";
  4.     return Regex.IsMatch(s,pattern);
  5. }
  6. /// <summary>
  7. /// 判断一个字符串是否为合法数字(0-32整数)
  8. /// </summary>
  9. /// <param name="s">字符串</param>
  10. /// <returns></returns>
  11. public static bool IsNumber(string s)
  12. {
  13.     return IsNumber(s,32,0);
  14. }
  15. /// <summary>
  16. /// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
  17. /// </summary>
  18. /// <param name="s">字符串</param>
  19. /// <param name="precision">整数位数</param>
  20. /// <param name="scale">小数位数</param>
  21. /// <returns></returns>
  22. public static bool IsNumber(string s,int precision,int scale)
  23. {
  24.     if((precision == 0)&&(scale == 0))
  25.     {
  26.         return false;
  27.     }
  28.     string pattern = @"(^\d{1,"+precision+"}";
  29.     if(scale>0)
  30.     {
  31.         pattern += @"\.\d{0,"+scale+"}$)|"+pattern;
  32.     }
  33.     pattern += "$)";
  34.     return Regex.IsMatch(s,pattern);
  35. }
posted on 2012-02-08 11:06  武胜-阿伟  阅读(16155)  评论(0)    收藏  举报