StringUtils.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. namespace FL
  3. {
  4. public static class StringUtils
  5. {
  6. /// <summary>
  7. /// 毫秒转指定格式的时间字符串
  8. /// </summary>
  9. /// <param name="milliseconds"></param>
  10. /// <param name="format">字符串格式, 例: @"mm\:ss"</param>
  11. /// <returns></returns>
  12. public static string ToTimeString(this long milliseconds, string format)
  13. {
  14. var timeData = TimeSpan.FromMilliseconds(milliseconds);
  15. return timeData.ToString(format);
  16. }
  17. private const string MILLION = "M";
  18. private const string THOUSAND = "K";
  19. /// <summary>
  20. /// 货币、战力数字转字符串
  21. /// >=百万,以M结尾
  22. /// >=万,以K结尾
  23. /// </summary>
  24. /// <param name="number"></param>
  25. /// <param name="decimals">保留小数点位数</param>
  26. /// <returns></returns>
  27. public static string FormatNumber(this long number, int decimals = 1)
  28. {
  29. const int million = 1000000;
  30. const int thousand = 1000;
  31. var format = $"{{0:N{decimals}}}";
  32. if (number >= million)
  33. {
  34. return string.Format(format, (double)number / million) + MILLION;
  35. }
  36. if (number >= thousand * 10)
  37. {
  38. return string.Format(format, (double)number / thousand) + THOUSAND;
  39. }
  40. return number.ToString();
  41. }
  42. }
  43. }