[objective-c]格式化数字显示数千和小数分隔符

标签: ios Objective-C
发布时间: 2016/10/2 1:34:36
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想让数字像这要像这 123,456.56 美元的 123456.567 和 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 中设置有千 sepator 和每个币种代码的十进制小数的数字。 => 2 分数美元其他货币 => 3 的分数

解决方法 1:

我弄清楚如何去做

if ([toCurrency.Code isEqualToString:@"USD"]) {
    [formatter setFormatWidth:2];
    formatter.maximumFractionDigits = 3;
    formatter.minimumFractionDigits = 3;
    formattedString = [formatter stringFromNumber:[NSNumber numberWithDouble:result]];

    [resultLbl setText:formattedString];
}else
{
    [formatter setFormatWidth:3];
    formatter.maximumFractionDigits = 2;
    formatter.minimumFractionDigits = 2;
    formattedString = [formatter stringFromNumber:[NSNumber numberWithDouble:result]];
    [resultLbl setText:formattedString];
}
官方微信
官方QQ群
31647020