格式化毫秒数/毫秒转字符串

/// <summary>
        /// 格式化毫秒数
        /// </summary>
        /// <param name="milliseconds"></param>
        /// <returns></returns>
        public static string FormatMilliseconds(long milliseconds)
        {
            try
            {
                List<Tuple<int, string>> lst = new List<Tuple<int, string>>()
                {
                    new Tuple<int, string>(86400000,""),
                    new Tuple<int, string>(3600000,""),
                    new Tuple<int, string>(60000,""),
                    new Tuple<int, string>(1000,""),
                    new Tuple<int, string>(1,"毫秒")
                };
                StringBuilder str = new StringBuilder();
                foreach (var item in lst)
                {
                    if (milliseconds >= item.Item1)
                    {
                        var v = milliseconds / item.Item1;
                        str.Append($"{v}{item.Item2}");
                        milliseconds -= v * item.Item1;
                    }
                }
                return str.ToString();
            }
            catch (Exception ex)
            {
                ErrorLog($"毫秒转换异常,{milliseconds},{ex.Message}{Environment.NewLine}{ex.StackTrace}");
                return $"{milliseconds}毫秒";
            }
        }

 

posted @ 2023-07-26 16:26  吃奶嘴的路飞  阅读(8)  评论(0编辑  收藏  举报