【问题标题】:Format a number to show thousands and decimal separators格式化数字以显示千位和小数分隔符
【发布时间】:2013-01-07 15:17:20
【问题描述】:

我正在尝试将 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 分数

【问题讨论】:

  • 对于任何货币都不要使用双精度。使用十进制类型,它是正确的格式。双重格式会导致操作过程中的资金损失。
  • 双倍对我来说工作正常,所以以美元计算的结果将为 7384.550 但如果我想更改为 nsnumber,我需要将其显示为 7,384.550,我无法将其显示为 7,384.550 它显示为 7,384.55
  • 在处理金钱时不应该使用浮点数。仅仅因为它适用于您的测试,并不意味着它总是正确的!请参阅:blog.plataformatec.com.br/2014/09/floating-point-and-currency

标签: ios objective-c formatting


【解决方案1】:

这里是修复。在您检查美元的格式化程序中,如果 USD 将 formatWidth 设置为 2,否则设置为 3

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setUsesGroupingSeparator:YES];

NSString* formattedString;

if (toCurrency.SellRate == 0) {
    result = 0;
}
if ([toCurrency.Code isEqualToString:@"USD"]) {
    [formatter setFormatWidth:2];
    formattedString = [formatter stringFromNumber:[NSNumber numberWithDouble:result]];
    [resultLbl setText:[NSString stringWithFormat:@"%@ %0.2f", toCurrency.Code, result]];     
}else
{
    [formatter setFormatWidth:3];
    formattedString = [formatter stringFromNumber:[NSNumber numberWithDouble:result]];
    [resultLbl setText:[NSString stringWithFormat:@"%@ %0.3f", toCurrency.Code, result]]; 

}

setFormat 似乎出现在 ios 文档中,即使它不起作用。

样本来自:

NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
NSMutableDictionary *newAttrs = [NSMutableDictionary dictionary];

[numberFormatter setFormat:@"$#,##0.00;($#,##0.00)"];
[newAttrs setObject:[NSColor redColor] forKey:@"NSColor"];
[numberFormatter setTextAttributesForNegativeValues:newAttrs];
[[textField cell] setFormatter:numberFormatter];

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html

【讨论】:

  • 有条件地设置 setFormatWidth 为我解决了这个问题。奇怪的是,Apple 的文档只使用了 setFormat。抱歉弄错了。
【解决方案2】:

我知道怎么做

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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多