首页 > 代码库 > 数字转字符串,带千位符(逗号)

数字转字符串,带千位符(逗号)

float转string
带千位符 ( 逗号 )
默认为.2f格式,需要其他请自行修改

[1].[代码] 自行更改string格式 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- (NSString*) numberToMathString:(float) num
{
    NSArray * array = [[NSString stringWithFormat:@"%.2f",num/100] componentsSeparatedByString:@"."];
    
    intlen = [array[0] length];
    intx = len%3;
    inty = len/3;
    intdotNumber = y;
    
    if(x == 0)
    {
        dotNumber -= 1;
        x = 3;
    }
    NSMutableString * rs = [@""mutableCopy];
    
    [rs appendString:[array[0] substringWithRange:NSMakeRange(0, x)]];
    
    for(int i=0; i<dotNumber; i++)
    {
        [rs appendString:@","];
        [rs appendString:[array[0] substringWithRange:NSMakeRange(x + i*3, 3)]];
    }
    [rs appendString:@"."];
    [rs appendString:array[1]];
    
    returnrs;
}

数字转字符串,带千位符(逗号)