我正在尝试将这样的 123456.567 数字设为美元的 123,456.56 和任何其他货币的 123,456.567
我的代码是:
double fromAgainstJOD = amountVal * fromCurrency.SellRate;
double result = fromAgainstJOD / toCurrency.SellRate;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setUsesGroupingSeparator:YES];
NSString* formattedString = [formatter stringFromNumber:[NSNumber numberWithDouble:result]];
if (toCurrency.SellRate == 0) {
result = 0;
}
if ([toCurrency.Code isEqualToString:@"USD"]) {
[resultLbl setText:[NSString stringWithFormat:@"%@ %0.2f", toCurrency.Code, result]];
} else {
[resultLbl setText:[NSString stringWithFormat:@"%@ %0.3f", toCurrency.Code, result]];
}
所以我想在 resultLbl 中为每个货币代码设置带有千位分隔符和小数部分的数字。美元 => 2 分数其他货币 => 3 分数