using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NCC.Code { public static class ConvertExtension { public static string M5_ToString(this object value) { return value == null ? string.Empty : value.ToString().Trim(); } public static bool IsNull(this object obj) { return obj == null; } public static bool IsNotNullAndHasCount(this List obj) { try { if (obj != null && obj.Count > 0) return true; } catch { } return false; } public static bool IsNotNullAndHasCount(this List obj) { try { if (obj != null && obj.Count > 0) return true; } catch { } return false; } /// /// 判断是否有交集 /// /// /// /// /// public static bool IsArrayIntersection(this List list1, List list2) { List t = list1.Distinct().ToList(); var exceptArr = t.Except(list2).ToList(); if (exceptArr.Count < t.Count) { return true; } else { return false; } } public static bool IsNotNullAndHasCount(this List obj) { try { if (obj != null && obj.Count > 0) return true; } catch { } return false; } public static Guid Parse(this string guid) { var gid = new Guid(guid); return gid; } /// /// 时间转换 /// /// /// public static DateTime ConvertDateTime(this string timeStr) { if (string.IsNullOrWhiteSpace(timeStr)) { return new DateTime(1990, 1, 1); } DateTime dt; DateTime.TryParse(timeStr, out dt); return dt; } public static int ParseInt(this string str) { if (string.IsNullOrWhiteSpace(str)) { return 0; } int a; int.TryParse(str, out a); return a; } public static int M5_ParseInt(this object str) { if (str == null) { return 0; } int a; int.TryParse(str.ToString(), out a); return a; } public static decimal M5_ParseDecimal(this object str) { if (str == null) { return 0; } decimal a; decimal.TryParse(str.ToString(), out a); return a; } /// /// 保留几位小数 /// /// /// 默认俩ing为 /// public static decimal MathDecimal(this decimal str, int len = 2) { try { return Math.Round(str, len); } catch (Exception) { return str; } } /// /// 实现数据的四舍五入法 /// /// 要进行处理的数据 /// 保留的小数位数 /// 四舍五入后的结果 public static decimal Round(this decimal v, int x = 0) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.Round(v * IValue, 0); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// /// 实现数据的向上取整 /// /// 要进行处理的数据 /// 保留的小数位数 /// 向上取整后的结果 public static decimal Ceiling(this decimal v, int x = 0) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.Ceiling(v * IValue); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// /// 实现数据的向下取整 /// /// 要进行处理的数据 /// 保留的小数位数 /// 向下取整后的结果 public static decimal Floor(this decimal v, int x = 0) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var Int = Math.Floor(v * IValue); v = Int / IValue; if (isNegative) { v = -v; } return v; } /// /// 实现见角进元 /// /// 要进行处理的数据 /// 保留的小数位数 /// 见角进元后的结果 public static decimal SeeDownUp(this decimal v, int x = 0) { var isNegative = false; //如果是负数 if (v < 0) { isNegative = true; v = -v; } var IValue = 1; for (var i = 1; i <= x; i++) { IValue = IValue * 10; } var stirngpanduan = Math.Floor(v * IValue * 10).ToString(); if (stirngpanduan.Substring(stirngpanduan.Length - 1) == "0") { var Int = Math.Floor(v * IValue); v = Int / IValue; } else { var Int = Math.Ceiling(v * IValue); v = Int / IValue; } if (isNegative) { v = -v; } return v; } /// /// 实现数据的四舍五入,向上取整,向下取整 /// /// 要进行处理的数据 /// 保留的小数位数 /// 1四舍五入,2向上取整,3向下取整 /// decimal public static decimal DecimalProcessing(this decimal v, int x, string type) { decimal result = 0; switch (type) { case "1": result = Round(v, x); break; case "2": result = Ceiling(v, x); break; case "3": result = Floor(v, x); break; case "4": result = SeeDownUp(v, x); break; default: result = Round(v, x); break; } return result; } /// /// 保留几位小数 来验证是否相等 /// /// /// 对比 /// 默认俩ing为 /// public static bool MathDecimalContrast(this decimal str, decimal com, int len = 2) { try { return Math.Round(str, len) == Math.Round(com, len); } catch (Exception) { return false; } } public static double M5_ParseDouble(this object str) { if (str == null) { return 0; } double a; double.TryParse(str.ToString(), out a); return a; } public static DateTime M5_ParseDatetime(this string str) { if (str == null) { return new DateTime(1990, 1, 1); } DateTime a; DateTime.TryParse(str.ToString(), out a); return a; } } }