using System; namespace Masuit.Tools { public static class ValueTypeConvertExtensions { /// /// 字符串转int /// /// 源字符串 /// 转换失败的默认值 /// int类型的数字 public static int ToInt32(this string s, int defaultValue = 0) { return s.TryConvertTo(defaultValue); } /// /// 字符串转long /// /// 源字符串 /// 转换失败的默认值 /// int类型的数字 public static long ToInt64(this string s, long defaultValue = 0) { return s.TryConvertTo(defaultValue); } /// /// 字符串转double /// /// 源字符串 /// 转换失败的默认值 /// double类型的数据 public static double ToDouble(this string s, double defaultValue = 0) { return s.TryConvertTo(defaultValue); } /// /// 字符串转decimal /// /// 源字符串 /// 转换失败的默认值 /// int类型的数字 public static decimal ToDecimal(this string s, decimal defaultValue = 0) { return s.TryConvertTo(defaultValue); } /// /// 转decimal /// /// /// int类型的数字 public static decimal ToDecimal(this double s) { return new decimal(s); } /// /// 转double /// /// /// double类型的数据 public static double ToDouble(this decimal s) { return (double)s; } /// /// 将double转换成int /// /// double类型 /// int类型 public static int ToInt32(this double num) { return (int)Math.Floor(num); } /// /// 将double转换成int /// /// double类型 /// int类型 public static int ToInt32(this decimal num) { return (int)Math.Floor(num); } /// /// 将int转换成double /// /// int类型 /// int类型 public static double ToDouble(this int num) { return num * 1.0; } /// /// 将int转换成decimal /// /// int类型 /// int类型 public static decimal ToDecimal(this int num) { return new decimal(num); } } }