00-Extensions.Convert.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace LeaRun.Util.Extension
  5. {
  6. /// <summary>
  7. /// 类型转换扩展
  8. /// </summary>
  9. public static partial class Extensions
  10. {
  11. #region 数值转换
  12. /// <summary>
  13. /// 转换为整型
  14. /// </summary>
  15. /// <param name="data">数据</param>
  16. public static int ToInt(this object data)
  17. {
  18. if (data == null)
  19. return 0;
  20. int result;
  21. var success = int.TryParse(data.ToString(), out result);
  22. if (success)
  23. return result;
  24. try
  25. {
  26. return Convert.ToInt32(ToDouble(data, 0));
  27. }
  28. catch (Exception)
  29. {
  30. return 0;
  31. }
  32. }
  33. /// <summary>
  34. /// 转换为可空整型
  35. /// </summary>
  36. /// <param name="data">数据</param>
  37. public static int? ToIntOrNull(this object data)
  38. {
  39. if (data == null)
  40. return null;
  41. int result;
  42. bool isValid = int.TryParse(data.ToString(), out result);
  43. if (isValid)
  44. return result;
  45. return null;
  46. }
  47. /// <summary>
  48. /// 转换为双精度浮点数
  49. /// </summary>
  50. /// <param name="data">数据</param>
  51. public static double ToDouble(this object data)
  52. {
  53. if (data == null)
  54. return 0;
  55. double result;
  56. return double.TryParse(data.ToString(), out result) ? result : 0;
  57. }
  58. /// <summary>
  59. /// 转换为双精度浮点数,并按指定的小数位4舍5入
  60. /// </summary>
  61. /// <param name="data">数据</param>
  62. /// <param name="digits">小数位数</param>
  63. public static double ToDouble(this object data, int digits)
  64. {
  65. return Math.Round(ToDouble(data), digits);
  66. }
  67. /// <summary>
  68. /// 转换为可空双精度浮点数
  69. /// </summary>
  70. /// <param name="data">数据</param>
  71. public static double? ToDoubleOrNull(this object data)
  72. {
  73. if (data == null)
  74. return null;
  75. double result;
  76. bool isValid = double.TryParse(data.ToString(), out result);
  77. if (isValid)
  78. return result;
  79. return null;
  80. }
  81. /// <summary>
  82. /// 转换为高精度浮点数
  83. /// </summary>
  84. /// <param name="data">数据</param>
  85. public static decimal ToDecimal(this object data)
  86. {
  87. if (data == null)
  88. return 0;
  89. decimal result;
  90. return decimal.TryParse(data.ToString(), out result) ? result : 0;
  91. }
  92. /// <summary>
  93. /// 转换为高精度浮点数,并按指定的小数位4舍5入
  94. /// </summary>
  95. /// <param name="data">数据</param>
  96. /// <param name="digits">小数位数</param>
  97. public static decimal ToDecimal(this object data, int digits)
  98. {
  99. return Math.Round(ToDecimal(data), digits);
  100. }
  101. /// <summary>
  102. /// 转换为可空高精度浮点数
  103. /// </summary>
  104. /// <param name="data">数据</param>
  105. public static decimal? ToDecimalOrNull(this object data)
  106. {
  107. if (data == null)
  108. return null;
  109. decimal result;
  110. bool isValid = decimal.TryParse(data.ToString(), out result);
  111. if (isValid)
  112. return result;
  113. return null;
  114. }
  115. /// <summary>
  116. /// 转换为可空高精度浮点数,并按指定的小数位4舍5入
  117. /// </summary>
  118. /// <param name="data">数据</param>
  119. /// <param name="digits">小数位数</param>
  120. public static decimal? ToDecimalOrNull(this object data, int digits)
  121. {
  122. var result = ToDecimalOrNull(data);
  123. if (result == null)
  124. return null;
  125. return Math.Round(result.Value, digits);
  126. }
  127. #endregion
  128. #region 日期转换
  129. /// <summary>
  130. /// 转换为日期
  131. /// </summary>
  132. /// <param name="data">数据</param>
  133. public static DateTime ToDate(this object data)
  134. {
  135. if (data == null)
  136. return DateTime.MinValue;
  137. DateTime result;
  138. return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
  139. }
  140. /// <summary>
  141. /// 转换为可空日期
  142. /// </summary>
  143. /// <param name="data">数据</param>
  144. public static DateTime? ToDateOrNull(this object data)
  145. {
  146. if (data == null)
  147. return null;
  148. DateTime result;
  149. bool isValid = DateTime.TryParse(data.ToString(), out result);
  150. if (isValid)
  151. return result;
  152. return null;
  153. }
  154. #endregion
  155. #region 布尔转换
  156. /// <summary>
  157. /// 转换为布尔值
  158. /// </summary>
  159. /// <param name="data">数据</param>
  160. public static bool ToBool(this object data)
  161. {
  162. if (data == null)
  163. return false;
  164. bool? value = GetBool(data);
  165. if (value != null)
  166. return value.Value;
  167. bool result;
  168. return bool.TryParse(data.ToString(), out result) && result;
  169. }
  170. /// <summary>
  171. /// 获取布尔值
  172. /// </summary>
  173. private static bool? GetBool(this object data)
  174. {
  175. switch (data.ToString().Trim().ToLower())
  176. {
  177. case "0":
  178. return false;
  179. case "1":
  180. return true;
  181. case "是":
  182. return true;
  183. case "否":
  184. return false;
  185. case "yes":
  186. return true;
  187. case "no":
  188. return false;
  189. default:
  190. return null;
  191. }
  192. }
  193. /// <summary>
  194. /// 转换为可空布尔值
  195. /// </summary>
  196. /// <param name="data">数据</param>
  197. public static bool? ToBoolOrNull(this object data)
  198. {
  199. if (data == null)
  200. return null;
  201. bool? value = GetBool(data);
  202. if (value != null)
  203. return value.Value;
  204. bool result;
  205. bool isValid = bool.TryParse(data.ToString(), out result);
  206. if (isValid)
  207. return result;
  208. return null;
  209. }
  210. #endregion
  211. #region 字符串转换
  212. /// <summary>
  213. /// 转换为字符串
  214. /// </summary>
  215. /// <param name="data">数据</param>
  216. public static string ToString(this object data)
  217. {
  218. return data == null ? string.Empty : data.ToString().Trim();
  219. }
  220. #endregion
  221. }
  222. }