如何将 float 转换为带有千位分隔符和 2 个小数点的 NSString?

How to convert float to NSString with thousand separator and 2 decimal points?

我想将 floatvalue 转换为带有千位分隔符和 2 个小数点的 NSString,但没有成功。附上我的代码:- 有什么错误吗?

NSString *str = [NSString stringWithFormat:@"%.2f", [_goodPrice floatValue]];
NSLog(@"166 - %@",str); //Result = 900000.00

NSNumber *myNumber = @((int)([_goodPrice floatValue]*100)/100.0);
NSLog(@"169 - %@",myNumber); //Result = 900000

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:[_goodPrice floatValue]]];
NSLog(@"Result...%@",numberString);//Result = 900,000. HOW CAN I MAKE IT 900,000.00   ???
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencySymbol:@""]; //To remove currency symbol
NSLog(@"%@", [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:[_goodPrice floatValue]]]);

请根据您的要求使用NSNumberFormatterCurrencyStyle

NSString *str = [NSString stringWithFormat:@"%.2f", [_goodPrice floatValue]];
NSLog(@"166 - %@",str);

NSNumber *myNumber = @((int)([_goodPrice floatValue]*100)/100.0);
NSLog(@"169 - %@",myNumber);

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencySymbol:@""];
NSNumber *num = [NSNumber numberWithDouble:[str doubleValue]];
NSLog(@"Result...%@",[numberFormatter stringFromNumber:num]);