UtilConvert.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ropin.Inspection.Common.Helper
  7. {
  8. public static class UtilConvert
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. /// <param name="thisValue"></param>
  14. /// <returns></returns>
  15. public static int ObjToInt(this object thisValue)
  16. {
  17. int reval = 0;
  18. if (thisValue == null) return 0;
  19. if (thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  20. {
  21. return reval;
  22. }
  23. return reval;
  24. }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="thisValue"></param>
  29. /// <param name="errorValue"></param>
  30. /// <returns></returns>
  31. public static int ObjToInt(this object thisValue, int errorValue)
  32. {
  33. int reval = 0;
  34. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  35. {
  36. return reval;
  37. }
  38. return errorValue;
  39. }
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. /// <param name="thisValue"></param>
  44. /// <returns></returns>
  45. public static double ObjToMoney(this object thisValue)
  46. {
  47. double reval = 0;
  48. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  49. {
  50. return reval;
  51. }
  52. return 0;
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. /// <param name="thisValue"></param>
  58. /// <param name="errorValue"></param>
  59. /// <returns></returns>
  60. public static double ObjToMoney(this object thisValue, double errorValue)
  61. {
  62. double reval = 0;
  63. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  64. {
  65. return reval;
  66. }
  67. return errorValue;
  68. }
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. /// <param name="thisValue"></param>
  73. /// <returns></returns>
  74. public static string ObjToString(this object thisValue)
  75. {
  76. if (thisValue != null) return thisValue.ToString().Trim();
  77. return "";
  78. }
  79. /// <summary>
  80. ///
  81. /// </summary>
  82. /// <param name="thisValue"></param>
  83. /// <returns></returns>
  84. public static bool IsNotEmptyOrNull(this object thisValue)
  85. {
  86. return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null";
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="thisValue"></param>
  92. /// <param name="errorValue"></param>
  93. /// <returns></returns>
  94. public static string ObjToString(this object thisValue, string errorValue)
  95. {
  96. if (thisValue != null) return thisValue.ToString().Trim();
  97. return errorValue;
  98. }
  99. /// <summary>
  100. ///
  101. /// </summary>
  102. /// <param name="thisValue"></param>
  103. /// <returns></returns>
  104. public static Decimal ObjToDecimal(this object thisValue)
  105. {
  106. Decimal reval = 0;
  107. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  108. {
  109. return reval;
  110. }
  111. return 0;
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. /// <param name="thisValue"></param>
  117. /// <param name="errorValue"></param>
  118. /// <returns></returns>
  119. public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
  120. {
  121. Decimal reval = 0;
  122. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  123. {
  124. return reval;
  125. }
  126. return errorValue;
  127. }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. /// <param name="thisValue"></param>
  132. /// <returns></returns>
  133. public static DateTime ObjToDate(this object thisValue)
  134. {
  135. DateTime reval = DateTime.MinValue;
  136. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  137. {
  138. reval = Convert.ToDateTime(thisValue);
  139. }
  140. return reval;
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. /// <param name="thisValue"></param>
  146. /// <param name="errorValue"></param>
  147. /// <returns></returns>
  148. public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
  149. {
  150. DateTime reval = DateTime.MinValue;
  151. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  152. {
  153. return reval;
  154. }
  155. return errorValue;
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. /// <param name="thisValue"></param>
  161. /// <returns></returns>
  162. public static bool ObjToBool(this object thisValue)
  163. {
  164. bool reval = false;
  165. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  166. {
  167. return reval;
  168. }
  169. return reval;
  170. }
  171. /// <summary>
  172. /// 获取当前时间的时间戳
  173. /// </summary>
  174. /// <param name="thisValue"></param>
  175. /// <returns></returns>
  176. public static string DateToTimeStamp(this DateTime thisValue)
  177. {
  178. TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  179. return Convert.ToInt64(ts.TotalSeconds).ToString();
  180. }
  181. }
  182. }