JXCountFormatter.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // JXCountFormatter.m
  3. // AICity
  4. //
  5. // Feature: 003-ios-api-https
  6. // 数字格式化工具类实现
  7. //
  8. #import "JXCountFormatter.h"
  9. @implementation JXCountFormatter
  10. + (NSString *)formatCount:(long long)count {
  11. // 处理负数
  12. if (count < 0) {
  13. return [self formatCount:labs(count)];
  14. }
  15. // 小于1万,直接显示
  16. if (count < 10000) {
  17. return [NSString stringWithFormat:@"%lld", count];
  18. }
  19. // 1万 - 1亿之间,显示"万"
  20. if (count < 100000000) {
  21. return [self formatWan:count];
  22. }
  23. // 大于1亿,显示"亿"
  24. return [self formatYi:count];
  25. }
  26. + (NSString *)formatWan:(long long)count {
  27. double wan = count / 10000.0;
  28. if (wan < 10) {
  29. return [NSString stringWithFormat:@"%.1f万", wan];
  30. } else {
  31. return [NSString stringWithFormat:@"%.1f万", wan];
  32. }
  33. }
  34. + (NSString *)formatYi:(long long)count {
  35. double yi = count / 100000000.0;
  36. return [NSString stringWithFormat:@"%.1f亿", yi];
  37. }
  38. + (NSString *)formatCountCompact:(long long)count {
  39. // 处理负数
  40. if (count < 0) {
  41. return [self formatCountCompact:labs(count)];
  42. }
  43. // 小于1千,直接显示
  44. if (count < 1000) {
  45. return [NSString stringWithFormat:@"%lld", count];
  46. }
  47. // 1千 - 1百万之间,显示K
  48. if (count < 1000000) {
  49. double k = count / 1000.0;
  50. return [NSString stringWithFormat:@"%.1fK", k];
  51. }
  52. // 1百万 - 10亿之间,显示M
  53. if (count < 1000000000) {
  54. double m = count / 1000000.0;
  55. return [NSString stringWithFormat:@"%.1fM", m];
  56. }
  57. // 大于10亿,显示B
  58. double b = count / 1000000000.0;
  59. return [NSString stringWithFormat:@"%.1fB", b];
  60. }
  61. + (NSString *)formatPlayCount:(long long)count {
  62. return [NSString stringWithFormat:@"%@播放", [self formatCount:count]];
  63. }
  64. + (NSString *)formatLikeCount:(long long)count {
  65. if (count == 0) {
  66. return @"点赞";
  67. }
  68. return [self formatCount:count];
  69. }
  70. + (NSString *)formatCommentCount:(long long)count {
  71. if (count == 0) {
  72. return @"评论";
  73. }
  74. return [self formatCount:count];
  75. }
  76. + (NSString *)formatFavoriteCount:(long long)count {
  77. if (count == 0) {
  78. return @"收藏";
  79. }
  80. return [self formatCount:count];
  81. }
  82. + (NSString *)formatShareCount:(long long)count {
  83. if (count == 0) {
  84. return @"分享";
  85. }
  86. return [self formatCount:count];
  87. }
  88. @end