StringUtils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using XGame.Database;
  3. namespace FL
  4. {
  5. public static class StringUtils
  6. {
  7. /// <summary>
  8. /// 毫秒转指定格式的时间字符串
  9. /// </summary>
  10. /// <param name="milliseconds"></param>
  11. /// <param name="format">字符串格式, 例: @"mm\:ss"</param>
  12. /// <returns></returns>
  13. public static string ToTimeString(this long milliseconds, string format)
  14. {
  15. var timeData = TimeSpan.FromMilliseconds(milliseconds);
  16. return timeData.ToString(format);
  17. }
  18. private const string MILLION = "M";
  19. private const string THOUSAND = "K";
  20. /// <summary>
  21. /// 货币、战力数字转字符串
  22. /// >=百万,以M结尾
  23. /// >=万,以K结尾
  24. /// </summary>
  25. /// <param name="number"></param>
  26. /// <param name="decimals">保留小数点位数</param>
  27. /// <returns></returns>
  28. public static string ToNumberString(this long number, int decimals = 2)
  29. {
  30. const int million = 1000000;
  31. const int thousand = 1000;
  32. var format = $"F{decimals}";//$"{{0:N{decimals}}}";
  33. if (number >= million)
  34. {
  35. if (number % million == 0)
  36. return (number / million).ToString() + MILLION;
  37. return ((double)number / million).ToString(format) + MILLION;
  38. }
  39. if (number >= thousand * 10)
  40. {
  41. if (number % thousand == 0)
  42. return (number / thousand).ToString() + THOUSAND;
  43. return ((double)number / thousand).ToString(format) + THOUSAND;
  44. }
  45. return number.ToString();
  46. }
  47. /// <summary>
  48. /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
  49. /// </summary>
  50. /// <param name="val"></param>
  51. /// <param name="decimals">保留小数点位数</param>
  52. /// <returns></returns>
  53. public static string ToPercentString(this int val, int decimals = 2)
  54. {
  55. const int TenThousand = 10000; // 万
  56. if (val % TenThousand < 50)
  57. { // 小数点4舍5入,小于0.005,尾数为xx.00%
  58. return (val * 0.0001f).ToString("P0");
  59. }
  60. return (val * 0.0001f).ToString($"P{decimals}");
  61. }
  62. /// <summary>
  63. /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
  64. /// </summary>
  65. /// <param name="val"></param>
  66. /// <param name="decimals">保留小数点位数</param>
  67. /// <returns></returns>
  68. public static string ToPercentString(this long val, int decimals = 2)
  69. {
  70. const int TenThousand = 10000; // 万
  71. if (val % TenThousand < 50)
  72. { // 小数点4舍5入,小于0.005,尾数为xx.00%
  73. return (val * 0.0001f).ToString("P0");
  74. }
  75. return (val * 0.0001f).ToString($"P{decimals}");
  76. }
  77. /// <summary>
  78. /// 时间戳转换成 xxxx-xx-xx xx:xx格式
  79. /// </summary>
  80. /// <param name="milliseconds"></param>
  81. /// <returns></returns>
  82. public static string ToTimeYMDHM(this long milliseconds)
  83. {
  84. DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeSeconds(milliseconds);
  85. string yearMonthDay = dateTime.ToString("yyyy-MM-dd hh:mm");
  86. return yearMonthDay;
  87. }
  88. /// <summary>
  89. /// 获取属性类型值
  90. /// </summary>
  91. /// <param name="attributeInfo"></param>
  92. /// <param name="attrVal"></param>
  93. /// <returns></returns>
  94. public static string GetAttrValueDesc(AttrDescTable attributeInfo, int attrVal)
  95. {
  96. return (attributeInfo.Num_type == 1 || attributeInfo.Id == (int)EAttributeType.AtkSpeed) ? attrVal.ToPercentString() :
  97. (attrVal == 0 ? "0" : attrVal.ToString());
  98. }
  99. }
  100. }