namespace Atomx.Utils.Extension
{
    public static class NumberExtension
    {
        /// <summary>
        /// 字符串转 int64位数字,转换失败则返回0
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static long ToLong(this string text)
        {
            long.TryParse(text, out long num);
            return num;
        }

        /// <summary>
        /// 字符转Int32位数字，转换失败则返回0
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 字符串转 int64位数字,转换失败则返回0
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static double ToDouble(this string text)
        {
            double.TryParse(text, out double num);
            return num;
        }

        /// <summary>
        /// 移除小数点后面的0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal RemoveZero(this decimal value)
        {
            var text = value.ToString();
            text = text.TrimEnd('0').TrimStart('.');
            return Convert.ToDecimal(text);
        }

        /// <summary>
        /// 删除无意义的0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        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();
        }

    }
}
