lonelysoul

Happy share, happy life.

导航

金额数字千位分隔符

NSLog(@"The  end  string:%@",[selfseparatedDigitStringWithStr:@"105000"]);

2013-05-15 17:19:26.470 ghfghfgh[7837:f803] The begin string:105000

2013-05-15 17:19:26.471 ghfghfgh[7837:f803] The  end  string:105,000

 

- (NSString *)separatedDigitStringWithStr:(NSString *)digitString

{

    NSLog(@"The begin string:%@",digitString);

    NSMutableString *processString = [[NSMutableString alloc]initWithString:digitString];

    

    // 1

    int location = processString.length - 3;

    NSMutableArray *processArray = [NSMutableArrayarray];

    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 = [NSMutableArrayarray];

    int k = 0;

    for (NSString *str in processArray)

    {

        k++;

        NSMutableString *tmp = [[NSMutableStringalloc]initWithString: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 = [NSMutableStringstring];

    for (int i = resultsArray.count - 1 ; i >= 0; i--)

    {

        NSString *tmp = [resultsArray objectAtIndex:i];

        [resultString appendString:tmp];

    }

//    NSLog(@"resultStr = %@",resultString);

    return resultString;

}

 

 

祝您愉快开心 ^_^

posted on 2013-05-15 17:24  lonelysoul  阅读(713)  评论(0编辑  收藏  举报