iOS 千分符

来源:互联网 发布:linux文件系统 编辑:程序博客网 时间:2024/04/29 00:19
01#import "Math.h"
02 
03@implementation Math
04 
05+(BOOL)isNumber:(char)c
06{
07    for(inti=0;i<9;i++)
08    {
09        if(i==c-'0') {
10            returnYES;
11        }
12    }
13    returnNO;
14}
15 
16+(NSString *)getQianFenFu:(NSString *)price
17{
18    if(!price)
19    {
20        returnNULL;
21    }
22     
23    if([self isNumber:[price characterAtIndex:0]])
24    {
25        NSMutableString *sb;
26        NSString *oldPrice=price;
27        [oldPrice retain];
28        intx=[oldPrice length]%3;
29        intxx=[oldPrice length]/3;
30        sb=[[NSMutableString alloc]init];
31         
32        NSString *s1=[[oldPrice substringToIndex:x]stringByAppendingFormat:@","];
33        [sb appendString:s1];
34         
35        for(inti=0;i<xx;i++)
36        {
37            [sb appendString:[[oldPrice substringWithRange:NSMakeRange((x+(3*i)),3)]stringByAppendingFormat:@","]];
38        }
39        NSString *str=[NSString stringWithFormat:sb];
40        [sb release];
41        [oldPrice release];
42         
43        for(inti=0; i<[str length]; i++)
44        {
45            charc1=[str characterAtIndex:0];
46            charc2=[str characterAtIndex:[str length]-1];
47             
48            if([self isNumber:c1] && [self isNumber:c2])
49            {}
50            elseif([self isNumber:c1])
51            {
52                str =[str substringWithRange:NSMakeRange(0, [str length]-1)];
53            }
54            elseif([self isNumber:c2])
55            {
56                str =[str substringWithRange:NSMakeRange(1, [str length])];
57            }
58            else
59            {
60                str =[str substringWithRange:NSMakeRange(1, [str length]-1)];
61            }
62        }
63        returnstr;
64    }
65     
66    returnNULL;
67}
68 
69@end








更简单的


+ (NSString *)getQianFenFu:(long long)number {
        if (number < 1000) {
                return [NSString stringWithFormat:@"%d", number];
        }
       
        return [[self getQianFenFu:number/1000] stringByAppendingFormat:@",%03d", number%1000];
}
0 0
原创粉丝点击