using System; namespace FL { public static class StringUtils { /// /// 毫秒转指定格式的时间字符串 /// /// /// 字符串格式, 例: @"mm\:ss" /// public static string ToTimeString(this long milliseconds, string format) { var timeData = TimeSpan.FromMilliseconds(milliseconds); return timeData.ToString(format); } private const string MILLION = "M"; private const string THOUSAND = "K"; /// /// 货币、战力数字转字符串 /// >=百万,以M结尾 /// >=万,以K结尾 /// /// /// 保留小数点位数 /// public static string FormatNumber(this long number, int decimals = 1) { const int million = 1000000; const int thousand = 1000; var format = $"{{0:N{decimals}}}"; if (number >= million) { return string.Format(format, (double)number / million) + MILLION; } if (number >= thousand * 10) { return string.Format(format, (double)number / thousand) + THOUSAND; } return number.ToString(); } } }