1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- using System;
- namespace FL
- {
- public static class StringUtils
- {
- /// <summary>
- /// 毫秒转指定格式的时间字符串
- /// </summary>
- /// <param name="milliseconds"></param>
- /// <param name="format">字符串格式, 例: @"mm\:ss"</param>
- /// <returns></returns>
- 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";
- /// <summary>
- /// 货币、战力数字转字符串
- /// >=百万,以M结尾
- /// >=万,以K结尾
- /// </summary>
- /// <param name="number"></param>
- /// <param name="decimals">保留小数点位数</param>
- /// <returns></returns>
- 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();
- }
- }
- }
|