using System; using System.Data.SqlTypes; namespace Nirvana.Common { public static partial class Ext { #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 long ToLong(this object data) { if (data == null) return 0; long result; return long.TryParse(data.ToString(), out result) ? result : 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; } /// /// 转换为float /// /// 数据 /// public static float ToFloat(this object data) { if (data == null) return 0; float result; return float.TryParse(data.ToString(), out result) ? result : 0; } /// /// 转换为双精度浮点数,并按指定的小数位4舍5入 /// /// 数据 /// 小数位数 public static double ToDouble(this object data, int digits) { var result = ToDouble(data); string numToString = result.ToString(); int index = numToString.IndexOf("."); int length = numToString.Length; if (index != -1) { numToString = string.Format("{0}.{1}", numToString.Substring(0, index), numToString.Substring(index + 1, Math.Min(length - index - 1, digits))); } return ToDouble(numToString); } /// /// 转换为可空双精度浮点数 /// /// 数据 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; string val = data.ToString().Replace(" ", ""); return decimal.TryParse(val, out result) ? result : 0; } /// /// 转换为高精度浮点数,不4舍5入 /// /// 数据 /// 小数位数 public static decimal ToDecimal(this object data, int digits) { var result = ToDecimal(data); string numToString = result.ToString(); int index = numToString.IndexOf("."); int length = numToString.Length; if (index != -1) { numToString = string.Format("{0}.{1}", numToString.Substring(0, index), numToString.Substring(index + 1, Math.Min(length - index - 1, digits))); } return ToDecimal(numToString); //return Math.Round(ToDecimal(numToString), digits); } /// /// 转换为高精度浮点数,并按指定的小数位4舍5入 /// /// 数据 /// 小数位数 public static decimal ToDecimal45(this object data, int digits) { //var result = ToDecimal(data); //string numToString = result.ToString(); //int index = numToString.IndexOf("."); //int length = numToString.Length; //if (index != -1) //{ // numToString = string.Format("{0}.{1}", // numToString.Substring(0, index), // numToString.Substring(index + 1, Math.Min(length - index - 1, digits))); //} //return ToDecimal(numToString); 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; } /// /// 时间格式化,带时分,如果同一年则不显示年,否则显示年份 /// /// /// public static string ToYearDateTime(this object data) { if (data == null) return string.Empty; DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (!isValid) return string.Empty; if (result == SqlDateTime.MinValue) { return "-"; } if (result.Year == DateTime.Now.Year) return result.ToString("M-d HH:mm"); return result.ToString("yyyy-M-d HH:mm"); } /// /// 时间格式化,带时分,如果同一年则不显示年,否则显示年份 /// /// /// public static string ToYearDateTimes(this object data) { if (data == null) return string.Empty; DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (!isValid) return string.Empty; if (result.Year == DateTime.Now.Year) return result.ToString("MM-dd HH:mm"); return result.ToString("yyyy-MM-dd HH:mm"); } /// /// 时间格式化,不带时分,如果同一年则不显示年,否则显示年份 /// /// /// public static string ToYearDate(this object data) { if (data == null) return string.Empty; DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (!isValid) return string.Empty; if (result.Year == DateTime.Now.Year) return result.ToString("M-d"); return result.ToString("yyyy-M-d"); } /// /// 时间格式化,只有年月,如果同一年则不显示年,否则显示年份 /// /// /// public static string ToYearMonth(this object data) { if (data == null) return string.Empty; DateTime result; bool isValid = DateTime.TryParse(data.ToString(), out result); if (!isValid) return string.Empty; if (result.Year == DateTime.Now.Year) return result.ToString("M"); return result.ToString("yyyy-M"); } #endregion 日期转换 #region 布尔转换 /// /// 转换为布尔值 /// /// 数据 public static bool ToBool(this object data) { if (data == null) return false; bool? value = GetBool(data); if (value != null) return value.Value; bool result; return bool.TryParse(data.ToString(), out result) && result; } /// /// 获取布尔值 /// private static bool? GetBool(this object data) { switch (data.ToString().Trim().ToLower()) { case "0": return false; case "1": return true; case "是": return true; case "否": return false; case "yes": return true; case "no": return false; default: return null; } } /// /// 转换为可空布尔值 /// /// 数据 public static bool? ToBoolOrNull(this object data) { if (data == null) return null; bool? value = GetBool(data); if (value != null) return value.Value; bool result; bool isValid = bool.TryParse(data.ToString(), out result); if (isValid) return result; return null; } #endregion 布尔转换 #region Guid转换 /// /// 转换为Guid /// /// /// public static Guid ToGuid(this object data) { if (data == null) { return Guid.Empty; } Guid result; bool isValid = Guid.TryParse(data.ToString(), out result); if (isValid) return result; return Guid.Empty; } #endregion Guid转换 #region 字符串转换 /// /// 转换为字符串 /// /// 数据 public static string ToString(this object data) { return data == null ? string.Empty : data.ToString().Trim(); } /// /// 针对可能为空的字符串处理 /// /// /// public static string ToStr(this string data) { return string.IsNullOrWhiteSpace(data) ? string.Empty : data.Trim(); } /// /// 针对可能为空的字符串处理 /// /// /// public static string ToStrNoEmpty(this string data) { return string.IsNullOrWhiteSpace(data) ? string.Empty : data.Replace(" ", ""); } #endregion 字符串转换 #region 进制转换 /// /// 字节数组转16进制 /// /// /// public static string BytesToHexStr(this byte[] bt) { string returnStr = ""; if (bt != null) { for (int i = 0; i < bt.Length; i++) { returnStr += bt[i].ToString("X2") + " "; } } return returnStr; } /// /// byte转int /// /// /// public static int ByteToInt(this byte bt) { return Convert.ToInt32(((int)bt).ToString("X2"), 16); } /// /// 16进制转10进制 /// /// /// public static int HexToDesc(this string hexStr) { return Convert.ToInt32(hexStr, 16); } /// /// 字节转16进制 /// /// /// public static string ByteToHexStr(this byte bt) { return ((int)bt).ToString("X2"); } #endregion 进制转换 /// /// 安全返回值 /// /// 可空值 public static T SafeValue(this T? value) where T : struct { return value ?? default(T); } /// /// 是否为空 /// /// 值 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; } } /// /// 是否为空 /// /// /// public static bool IsEmpty(this DateTime? value) { if (value != null && value.HasValue) { return false; } return true; } } }