01 | #import "Math.h" |
02 |
03 | @implementation Math |
04 |
05 | +( BOOL )isNumber:( char )c |
06 | { |
07 | for ( int i=0;i<9;i++) |
08 | { |
09 | if (i==c- '0' ) { |
10 | return YES; |
11 | } |
12 | } |
13 | return NO; |
14 | } |
15 |
16 | +(NSString *)getQianFenFu:(NSString *)price |
17 | { |
18 | if (!price) |
19 | { |
20 | return NULL; |
21 | } |
22 | |
23 | if ([self isNumber:[price characterAtIndex:0]]) |
24 | { |
25 | NSMutableString *sb; |
26 | NSString *oldPrice=price; |
27 | [oldPrice retain]; |
28 | int x=[oldPrice length]%3; |
29 | int xx=[oldPrice length]/3; |
30 | sb=[[NSMutableString alloc]init]; |
31 | |
32 | NSString *s1=[[oldPrice substringToIndex:x]stringByAppendingFormat:@ "," ]; |
33 | [sb appendString:s1]; |
34 | |
35 | for ( int i=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 ( int
i=0; i<[str length]; i++) |
44 | { |
45 | char c1=[str characterAtIndex:0]; |
46 | char c2=[str characterAtIndex:[str length]-1]; |
47 | |
48 | if ([self isNumber:c1] && [self isNumber:c2]) |
49 | {} |
50 | else if ([self isNumber:c1]) |
51 | { |
52 | str =[str substringWithRange:NSMakeRange(0, [str length]-1)]; |
53 | } |
54 | else if ([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 | return str; |
64 | } |
65 | |
66 | return NULL; |
67 | } |
68 |
69 | @end |