数字的1000位显示
10000.00 ----10,000.00 目标效果
#import "NSString+MoneyFormat.h"
@implementation NSString (MoneyFormat)
+(NSString*)strmethodComma:(NSString*)digitString
{
if (digitString.length <= 3) {
return digitString;
} else {
NSMutableString *processString = [NSMutableString stringWithString:digitString];
// 1
int location = processString.length - 3;
NSMutableArray *processArray = [NSMutableArray array];
while (location >= 0) {
NSString *temp = [processString substringWithRange:NSMakeRange(location, 3)];
[processArray addObject:temp];
if (location < 3 && location > 0)
{
NSString *t = [processString substringWithRange:NSMakeRange(0, location)];
[processArray addObject:t];
}
location -= 3;
}
// NSLog(@"processArray = %@",processArray);
// 2
NSMutableArray *resultsArray = [NSMutableArray array];
int k = 0;
for (NSString *str in processArray)
{
k++;
NSMutableString *tmp = [NSMutableString stringWithString:str];
if (str.length > 2 && k < processArray.count )
{
[tmp insertString:@"," atIndex:0];
[resultsArray addObject:tmp];
} else {
[resultsArray addObject:tmp];
}
}
// NSLog(@"resultsArray = %@",resultsArray);
// 3
NSMutableString *resultString = [NSMutableString string];
for (int i = resultsArray.count - 1 ; i >= 0; i--)
{
NSString *tmp = [resultsArray objectAtIndex:i];
[resultString appendString:tmp];
}
// NSLog(@"resultStr = %@",resultString);
return resultString;
}
}
+(NSString *)formateMoneyInThree:(NSString *)str
{
NSInteger num = [str integerValue];
float floatNum = [str floatValue];
if ([str containsString:@"."]) {
NSMutableString * tem = [NSMutableString stringWithFormat:@"%.2f",floatNum - num];
NSMutableString * tem2 = [tem substringWithRange:NSMakeRange(1, tem.length-1)];
NSString * tem3 = [NSString strmethodComma:[NSString stringWithFormat:@"%ld",num]];
NSString * result = [NSString stringWithFormat:@"%@%@",tem3,tem2];
return result;
}
else
{
NSString * result = [NSString strmethodComma:[NSString stringWithFormat:@"%ld",num]];
return result;
}
}
+(NSString *)normalFormateMoney:(NSString *)str
{
if ([str containsString:@","]) {
NSArray * strArr = [str componentsSeparatedByString:@","];
NSMutableString * strTem = [NSMutableString string];
for (int i = 0; i < strArr.count; i++) {
[strTem appendString:strArr[i]];
}
return strTem;
} else {
return str;
}
}
@end