我正在尝试将我的浮动格式化为类似“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];
答案 0 :(得分:1)
您只需要将以下内容替换为
//it will change your value as double
NSNumber *myNum = [NSNumber numberWithDouble:3343000.55];
只需从numberWithInt
更改为numberWithDouble