using System; using System.Collections.Generic; using System.Text; namespace CommonUtil { /// /// 扩展方法工具类-高精度浮点数 /// public static partial class ExtendUtil { /// /// 转换为高精度浮点数,null会转为0 /// /// 数据 /// 高精度浮点数 public static decimal ToDecimal(this string data) { if (data.IsNullOrEmpty()) { return 0; } return decimal.TryParse(data, out decimal result) ? result : 0; } /// /// 转换为高精度浮点数,null会转为0 /// /// 数据 /// 高精度浮点数 public static decimal ToDecimal(this T data) where T : struct { return decimal.TryParse(data.ToString(), out decimal result) ? result : 0; } /// /// 转换为高精度浮点数,null会转为0 /// /// 数据 /// 高精度浮点数 public static decimal ToDecimal(this T? data) where T : struct { return data.HasValue ? ToDecimal(data.Value) : 0; } /// /// 转换为高精度浮点数,并按指定的小数位数四舍五入,null会转为0 /// /// 数据 /// 小数位数 /// 高精度浮点数 public static decimal ToDecimal(this string data, int digits = 2) { return Math.Round(ToDecimal(data), digits, MidpointRounding.AwayFromZero); } /// /// 转换为高精度浮点数,并按指定的小数位数四舍五入,null会转为0 /// /// 数据 /// 小数位数 /// 高精度浮点数 public static decimal ToDecimal(this T data, int digits = 2) where T : struct { return Math.Round(ToDecimal(data), digits, MidpointRounding.AwayFromZero); } /// /// 转换为高精度浮点数,并按指定的小数位数四舍五入,null会转为0 /// /// 数据 /// 小数位数 /// 高精度浮点数 public static decimal ToDecimal(this T? data, int digits = 2) where T : struct { return data.HasValue ? ToDecimal(data.Value, digits) : 0; } /// /// 转换为可空高精度浮点数 /// /// 数据 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this string data) { if (data == null) { return null; } bool isValid = decimal.TryParse(data, out decimal result); if (isValid) { return result; } return null; } /// /// 转换为可空高精度浮点数 /// /// 数据 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this T data) where T : struct { bool isValid = decimal.TryParse(data.ToString(), out decimal result); if (isValid) { return result; } return null; } /// /// 转换为可空高精度浮点数 /// /// 数据 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this T? data) where T : struct { return data.HasValue ? ToDecimalOrNull(data.Value) : null; } /// /// 转换为可空高精度浮点数,并按指定的小数位数四舍五入 /// /// 数据 /// 小数位数 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this string data, int digits = 2) { decimal? result = ToDecimalOrNull(data); if (!result.HasValue) { return null; } return Math.Round(result.Value, digits, MidpointRounding.AwayFromZero); } /// /// 转换为可空高精度浮点数,并按指定的小数位数四舍五入 /// /// 数据 /// 小数位数 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this T data, int digits = 2) where T : struct { decimal? result = ToDecimalOrNull(data.ToString()); if (!result.HasValue) { return null; } return Math.Round(result.Value, digits, MidpointRounding.AwayFromZero); } /// /// 转换为可空高精度浮点数,并按指定的小数位数四舍五入 /// /// 数据 /// 小数位数 /// 可空高精度浮点数 public static decimal? ToDecimalOrNull(this T? data, int digits = 2) where T : struct { return data.HasValue ? ToDecimalOrNull(data.Value, digits) : null; } } }