using System; namespace FineAdmin.Common { public static class ExtConvert { #region 数值转换 /// /// 转换为整型 /// /// 数据 public static int ToInt(this object data) { if (data == null) return 0; int result; var success = int.TryParse(data.ToString(), out result); if (success) return result; try { return Convert.ToInt32(ToDouble(data, 0)); } catch (Exception) { return 0; } } /// /// 转换为可空整型 /// /// 数据 public static int? ToIntOrNull(this object data) { if (data == null) return null; int result; bool isValid = int.TryParse(data.ToString(), out result); if (isValid) return result; return null; } /// /// 转换为双精度浮点数 /// /// 数据 public static double ToDouble(this object data) { if (data == null) return 0; double result; return double.TryParse(data.ToString(), out result) ? result : 0; } /// /// 转换为双精度浮点数,并按指定的小数位4舍5入 /// /// 数据 /// 小数位数 public static double ToDouble(this object data, int digits) { return Math.Round(ToDouble(data), digits); } /// /// 转换为可空双精度浮点数 /// /// 数据 public static double? ToDoubleOrNull(this object data) { if (data == null) return null; double result; bool isValid = double.TryParse(data.ToString(), out result); if (isValid) return result; return null; } /// /// 转换为高精度浮点数 /// /// 数据 public static decimal ToDecimal(this object data) { if (data == null) return 0; decimal result; return decimal.TryParse(data.ToString(), out result) ? result : 0; } /// /// 转换为高精度浮点数,并按指定的小数位4舍5入 /// /// 数据 /// 小数位数 public static decimal ToDecimal(this object data, int digits) { return Math.Round(ToDecimal(data), digits); } /// /// 转换为可空高精度浮点数 /// /// 数据 public static decimal? ToDecimalOrNull(this object data) { if (data == null) return null; decimal result; bool isValid = decimal.TryParse(data.ToString(), out result); if (isValid) return result; return null; } /// /// 转换为可空高精度浮点数,并按指定的小数位4舍5入 /// /// 数据 /// 小数位数 public static decimal? ToDecimalOrNull(this object data, int digits) { var result = ToDecimalOrNull(data); if (result == null) return null; return Math.Round(result.Value, digits); } #endregion #region 日期转换 /// /// 转换为日期 /// /// 数据 public static DateTime ToDate(this object data) { if (data == null) return DateTime.MinValue; DateTime result; return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue; } /// /// 转换为可空日期 /// /// 数据 public static DateTime? ToDateOrNull(this object data) { if (data == null) return null; DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (isValid) return result; return null; } #endregion /// /// 是否为空 /// /// 值 public static bool IsEmpty(this string value) { return string.IsNullOrWhiteSpace(value); } /// /// 是否为空 /// /// 值 public static bool IsEmpty(this Guid? value) { if (value == null) return true; return IsEmpty(value.Value); } /// /// 是否为空 /// /// 值 public static bool IsEmpty(this Guid value) { if (value == Guid.Empty) return true; return false; } /// /// 是否为空 /// /// 值 public static bool IsEmpty(this object value) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { return false; } else { return true; } } } }