| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // JXCountFormatter.m
- // AICity
- //
- // Feature: 003-ios-api-https
- // 数字格式化工具类实现
- //
- #import "JXCountFormatter.h"
- @implementation JXCountFormatter
- + (NSString *)formatCount:(long long)count {
- // 处理负数
- if (count < 0) {
- return [self formatCount:labs(count)];
- }
-
- // 小于1万,直接显示
- if (count < 10000) {
- return [NSString stringWithFormat:@"%lld", count];
- }
-
- // 1万 - 1亿之间,显示"万"
- if (count < 100000000) {
- return [self formatWan:count];
- }
-
- // 大于1亿,显示"亿"
- return [self formatYi:count];
- }
- + (NSString *)formatWan:(long long)count {
- double wan = count / 10000.0;
-
- if (wan < 10) {
- return [NSString stringWithFormat:@"%.1f万", wan];
- } else {
- return [NSString stringWithFormat:@"%.1f万", wan];
- }
- }
- + (NSString *)formatYi:(long long)count {
- double yi = count / 100000000.0;
- return [NSString stringWithFormat:@"%.1f亿", yi];
- }
- + (NSString *)formatCountCompact:(long long)count {
- // 处理负数
- if (count < 0) {
- return [self formatCountCompact:labs(count)];
- }
-
- // 小于1千,直接显示
- if (count < 1000) {
- return [NSString stringWithFormat:@"%lld", count];
- }
-
- // 1千 - 1百万之间,显示K
- if (count < 1000000) {
- double k = count / 1000.0;
- return [NSString stringWithFormat:@"%.1fK", k];
- }
-
- // 1百万 - 10亿之间,显示M
- if (count < 1000000000) {
- double m = count / 1000000.0;
- return [NSString stringWithFormat:@"%.1fM", m];
- }
-
- // 大于10亿,显示B
- double b = count / 1000000000.0;
- return [NSString stringWithFormat:@"%.1fB", b];
- }
- + (NSString *)formatPlayCount:(long long)count {
- return [NSString stringWithFormat:@"%@播放", [self formatCount:count]];
- }
- + (NSString *)formatLikeCount:(long long)count {
- if (count == 0) {
- return @"点赞";
- }
- return [self formatCount:count];
- }
- + (NSString *)formatCommentCount:(long long)count {
- if (count == 0) {
- return @"评论";
- }
- return [self formatCount:count];
- }
- + (NSString *)formatFavoriteCount:(long long)count {
- if (count == 0) {
- return @"收藏";
- }
- return [self formatCount:count];
- }
- + (NSString *)formatShareCount:(long long)count {
- if (count == 0) {
- return @"分享";
- }
- return [self formatCount:count];
- }
- @end
|