Objective c使用逗号和2位小数格式化float

时间:2016-11-17 09:58:27

标签: objective-c formatter

我正在尝试将我的浮动格式化为类似“3,343,000.55”的内容并将其转换为字符串。

使用下面的代码,我会得到类似“3,343,000.00”的内容。

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    numberFormatter.usesGroupingSeparator = YES;
    [numberFormatter setMaximumFractionDigits:2];
    [numberFormatter setMinimumFractionDigits:2];
    NSNumber *myNum = [NSNumber numberWithInt:myFloat];
    NSString *myFor = [numberFormatter stringFromNumber:myNum];

1 个答案:

答案 0 :(得分:1)

您只需要将以下内容替换为

//it will change your value as double
NSNumber *myNum = [NSNumber numberWithDouble:3343000.55];

只需从numberWithInt更改为numberWithDouble