iOS|iOS - 3位数添加一个逗号
【iOS|iOS - 3位数添加一个逗号】添加如下NSString类别,代码如下
:
.h
#import @interface NSString (Comma)+(NSString*)strmethodComma:(NSString*)string;
@end
.m
#import "NSString+Comma.h"@implementation NSString (Comma)+(NSString*)strmethodComma:(NSString*)str
{NSString *intStr;
NSString *floStr;
if ([str containsString:@"."]) {NSRange range = [str rangeOfString:@"."];
floStr = [str substringFromIndex:range.location];
intStr = [str substringToIndex:range.location];
}else{floStr = @"";
intStr = str;
}if (intStr.length <=3) {return [intStr stringByAppendingString:floStr];
}else{NSInteger length = intStr.length;
NSInteger count = length/3;
NSInteger y = length%3;
NSString *tit = [intStr substringToIndex:y] ;
NSMutableString *det = [[intStr substringFromIndex:y] mutableCopy];
for (int i =0;
i < count;
i ++) {NSInteger index = i + i *3;
[det insertString:@","atIndex:index];
}if (y ==0) {det = [[det substringFromIndex:1]mutableCopy];
}intStr = [tit stringByAppendingString:det];
return [intStr stringByAppendingString:floStr];
}
}
@end
简便方式请参考:https://www.jianshu.com/p/3689f115ff18
http://www.unicode.org/reports/tr35/tr35-31/tr35-numbers.html#Number_Format_Patterns
感谢!
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 编写字典程序
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- 怎么将桌面上的CAD图纸添加到软件中进行BMP格式转换()