namespace Atomx.Utils.Extension { public static class NumberExtension { /// /// 字符串转 int64位数字,转换失败则返回0 /// /// /// public static long ToLong(this string text) { long.TryParse(text, out long num); return num; } /// /// 字符转Int32位数字,转换失败则返回0 /// /// /// public static int ToInt(this string text) { int.TryParse(text, out int num); return num; } public static decimal ToDecimal(this string text) { decimal.TryParse(text, out decimal num); return num; } /// /// 字符串转 int64位数字,转换失败则返回0 /// /// /// public static double ToDouble(this string text) { double.TryParse(text, out double num); return num; } /// /// 移除小数点后面的0 /// /// /// public static decimal RemoveZero(this decimal value) { var text = value.ToString(); text = text.TrimEnd('0').TrimStart('.'); return Convert.ToDecimal(text); } /// /// 删除无意义的0 /// /// /// public static string RemoveTrailingZeros(this decimal value) { string str = value.ToString(); if (str.Contains('.')) { str = str.TrimEnd('0'); if (str.EndsWith(".")) { str = str.Substring(0, str.Length - 1); } } return str; } public static long DateToNumber(this DateTime data) { return data.ToString("yyyyMMddHHmmss").ToLong(); } } }