iPhone -SQLite:显示以逗号分隔的十进制值(“,”)

时间:2011-04-26 11:06:29

标签: iphone objective-c cocoa-touch sqlite ios4

在iPhone应用程序中,我想打印以“,”

分隔的十进制值

即.2392890应显示为2,392,890

我从SQLite数据库中获取这些数字

2 个答案:

答案 0 :(得分:0)

这是solution

//American number format - with comma sepparators between thousands
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormat:@"#,###"];
NSNumber *averageResponseTime = [NSNumber numberWithInt:123456789];

//If you print next thing
[NSString stringWithFormat: @"%@ms",[numberFormatter stringForObjectValue:averageResponseTime]];

//You will get 123,456,789

答案 1 :(得分:0)

将您的号码传递给以下功能以获得格式化输出。

-(NSString*)getFormattedString:(double)pDoubleValue
{
    NSNumber *doubleNumber = [NSNumber numberWithDouble:pDoubleValue];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *formattedOutput = [formatter stringFromNumber:doubleNumber];
    [formatter release];
    return formattedOutput;
}
希望它有所帮助。