纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.3 KiB

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