No Description

TypeConverter.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace CallCenter.Utility
  4. {
  5. /// <summary>
  6. /// 数据转换操作类
  7. /// </summary>
  8. public class TypeConverter
  9. {
  10. /// <summary>
  11. /// string型转换为bool型
  12. /// </summary>
  13. /// <param name="expression">要转换的字符串</param>
  14. /// <param name="defValue">缺省值</param>
  15. /// <returns>转换后的bool类型结果</returns>
  16. public static bool StrToBool(object expression, bool defValue)
  17. {
  18. if (expression != null)
  19. return StrToBool(expression, defValue);
  20. return defValue;
  21. }
  22. /// <summary>
  23. /// string型转换为bool型
  24. /// </summary>
  25. /// <param name="expression">要转换的字符串</param>
  26. /// <param name="defValue">缺省值</param>
  27. /// <returns>转换后的bool类型结果</returns>
  28. public static bool StrToBool(string expression, bool defValue)
  29. {
  30. if (expression != null)
  31. {
  32. if (String.Compare(expression, "true", StringComparison.OrdinalIgnoreCase) == 0)
  33. return true;
  34. if (String.Compare(expression, "false", StringComparison.OrdinalIgnoreCase) == 0)
  35. return false;
  36. }
  37. return defValue;
  38. }
  39. /// <summary>
  40. /// 将对象转换为Int32类型
  41. /// </summary>
  42. /// <param name="expression">要转换的字符串</param>
  43. /// <returns>转换后的int类型结果</returns>
  44. public static int ObjectToInt(object expression)
  45. {
  46. return ObjectToInt(expression, 0);
  47. }
  48. /// <summary>
  49. /// 将对象转换为Int32类型
  50. /// </summary>
  51. /// <param name="expression">要转换的字符串</param>
  52. /// <param name="defValue">缺省值</param>
  53. /// <returns>转换后的int类型结果</returns>
  54. public static int ObjectToInt(object expression, int defValue)
  55. {
  56. if (expression != null)
  57. return StrToInt(expression.ToString(), defValue);
  58. return defValue;
  59. }
  60. /// <summary>
  61. /// 将对象转换为Int32类型,转换失败返回0
  62. /// </summary>
  63. /// <param name="str">要转换的字符串</param>
  64. /// <returns>转换后的int类型结果</returns>
  65. public static int StrToInt(string str)
  66. {
  67. return StrToInt(str, 0);
  68. }
  69. /// <summary>
  70. /// 将对象转换为Int32类型
  71. /// </summary>
  72. /// <param name="str">要转换的字符串</param>
  73. /// <param name="defValue">缺省值</param>
  74. /// <returns>转换后的int类型结果</returns>
  75. public static int StrToInt(string str, int defValue)
  76. {
  77. if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
  78. return defValue;
  79. int rv;
  80. if (Int32.TryParse(str, out rv))
  81. return rv;
  82. return Convert.ToInt32(StrToFloat(str, defValue));
  83. }
  84. /// <summary>
  85. /// string型转换为float型
  86. /// </summary>
  87. /// <param name="strValue">要转换的字符串</param>
  88. /// <param name="defValue">缺省值</param>
  89. /// <returns>转换后的int类型结果</returns>
  90. public static float StrToFloat(object strValue, float defValue)
  91. {
  92. if ((strValue == null))
  93. return defValue;
  94. return StrToFloat(strValue.ToString(), defValue);
  95. }
  96. /// <summary>
  97. /// string型转换为float型
  98. /// </summary>
  99. /// <param name="strValue">要转换的字符串</param>
  100. /// <param name="defValue">缺省值</param>
  101. /// <returns>转换后的int类型结果</returns>
  102. public static float ObjectToFloat(object strValue, float defValue)
  103. {
  104. if ((strValue == null))
  105. return defValue;
  106. return StrToFloat(strValue.ToString(), defValue);
  107. }
  108. /// <summary>
  109. /// string型转换为float型
  110. /// </summary>
  111. /// <param name="strValue">要转换的字符串</param>
  112. /// <returns>转换后的int类型结果</returns>
  113. public static float ObjectToFloat(object strValue)
  114. {
  115. return ObjectToFloat(strValue.ToString(), 0);
  116. }
  117. /// <summary>
  118. /// string型转换为float型
  119. /// </summary>
  120. /// <param name="strValue">要转换的字符串</param>
  121. /// <returns>转换后的int类型结果</returns>
  122. public static float StrToFloat(object strValue)
  123. {
  124. if ((strValue == null))
  125. return 0;
  126. return StrToFloat(strValue.ToString(), 0);
  127. }
  128. /// <summary>
  129. /// string型转换为float型
  130. /// </summary>
  131. /// <param name="strValue">要转换的字符串</param>
  132. /// <param name="defValue">缺省值</param>
  133. /// <returns>转换后的int类型结果</returns>
  134. public static float StrToFloat(string strValue, float defValue)
  135. {
  136. if ((strValue == null) || (strValue.Length > 10))
  137. return defValue;
  138. float intValue = defValue;
  139. bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
  140. if (IsFloat)
  141. float.TryParse(strValue, out intValue);
  142. return intValue;
  143. }
  144. }
  145. }