I know this is very common question but I am really stuck in this matter.
我知道这是一个很常见的问题,但我真的被这个问题困住了。
I just want to covert Integer value to string using formatter.
我只是想用格式化程序将整数值转换为字符串。
Example like,
Suppose the integer value is like 1252
then It will convert into 1,252
and If like 1256256
then It will convert into 1,256,256
, It means to insert comma after every 3 digits..
例如,假设整数值是1252,那么它就会转换成12252,如果是1256256,那么它就会转换成12256256,这意味着每3个数字之后插入逗号。
Please help me to solve such a basic problem..
请帮我解决这个基本问题。
Thanks in advance..
提前谢谢. .
2 个解决方案
#1
6
You can use NSNumberFormatter class. Convert integer to NSNumber and use NSNumberFormatter.
您可以使用NSNumberFormatter类。将整数转换为NSNumber并使用NSNumberFormatter。
NSNumber *number = [NSNumber numberWithInt:1256256];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:@","];
[formatter setUsesGroupingSeparator:YES];
NSString *finalString = [formatter stringFromNumber:number];
#2
0
Check out the NSNumberFormatter
document about setNumberStyle:NSNumberFormatterDecimalStyle
.
查看关于setNumberStyle: nsnumberformatterdecimaldecimaldecimaldecimstyle文件)
#1
6
You can use NSNumberFormatter class. Convert integer to NSNumber and use NSNumberFormatter.
您可以使用NSNumberFormatter类。将整数转换为NSNumber并使用NSNumberFormatter。
NSNumber *number = [NSNumber numberWithInt:1256256];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:@","];
[formatter setUsesGroupingSeparator:YES];
NSString *finalString = [formatter stringFromNumber:number];
#2
0
Check out the NSNumberFormatter
document about setNumberStyle:NSNumberFormatterDecimalStyle
.
查看关于setNumberStyle: nsnumberformatterdecimaldecimaldecimaldecimstyle文件)