1

我尝试测试以下代码以从货币样式格式化的 UITextField 中获取双精度值(例如:$ 30,034.12 => 30034.12):

// Init and configure formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMaximumFractionDigits:2];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"us_US"];
[formatter setLocale:locale];
[formatter setGroupingSize:3];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setAllowsFloats:YES];

// Get a formatted string using the local currency formatter and then get a double
// vice-versa
NSNumber *amount = [NSNumber numberWithDouble:30034.12];

NSString *amountString = [formatter stringFromNumber:amount];
// Output here OK : @"$ 30,034.12"

double amountDouble = [[formatter numberFromString:amountString] doubleValue];
// Output here NOT OK : 0

有没有人有/解决过同样的问题?

谢谢 !

更新 :

@DanielBarden 和其他人。实际上,为了简化帖子,我没有包含指定从何处获取字符串的部分:文本字段。实际上,代码末尾之前的行应该是:

NSString *amountString = [textField text];

并且此文本字段先前已使用另一种方法的以下代码格式化(货币样式使用相同的格式化程序配置):

    // Init fromatter with style $ XXX,XXX.yy
    NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setMaximumFractionDigits:2];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"us_US"];
    [formatter setLocale:locale];
    [formatter setGroupingSize:3];
    [formatter setGroupingSeparator:@","];
    [formatter setUsesGroupingSeparator:YES];

    // Get the float value of the text field and format it
    textField.text = [formatter 
                      stringFromNumber:[NSNumber 
                                        numberWithDouble:[textField.text 
                                                         floatValue]]];

现在的问题是,当我执行 NSLog 时,我得到完全相同的字符串,但是当我将它们逐个字符与循环进行比较时,它表示 $ 之后的空格是文本字段上的“真实空间”,而不同的符号初始字符串(amountString 我在最初的帖子中尝试测试的那个...)。编码问题?

4

3 回答 3

1

我唯一能想到的是 $ 符号的问题。将格式化程序设置为货币样式需要 $ 符号,如果它不存在于字符串中,则返回 0

于 2011-11-22T23:55:31.683 回答
0

该代码对我来说似乎是正确的,Xcode 同意我的看法。我在两行中添加了您缺少的内容:

NSLog(@"%@",amountString);
NSLog(@"%f",amountDouble);

并且输出是正确的。我建议您检查如何记录值。

于 2011-11-22T23:09:57.937 回答
0

问题formatter.currencySymbol可能不是正确的“$”,即“USD”。所以解决方案是formatter.currencySymbol = "$"

let localeID = "th_TH" // Thailand
let correctCurrencySymbol = "฿" // "$" for US dollar
let formatter = NumberFormatter()
formatter.locale = Locale(identifier:localeID)
formatter.numberStyle = .currencyStyle
let currencySymbol = formatter.currencySymbol // "฿" for iOS 13, "THB" for iOS 12 or earlier versions
print("currencySymbol: \(currencySymbol)")

if (currencySymbol != correctCurrencySymbol) { // "USD" for US dollar
    formatter.currencySymbol = correctCurrencySymbol 
    print("【Error】fix formatter.currencySymbol from \(currencySymbol) to \(correctCurrencySymbol)")
}

let amount = -30.12
if let str = formatter.string(for: amount) {
   print("Currency format of \(amount) is \(str)") // -฿30.12
   // works OK until set correctCurrencySymbol
   if let amount1 = formatter.number(from: str)?.decimalValue {
       print("Comvert back to number is \(amount1)") // -30.12
   }
}
于 2020-04-07T13:31:15.790 回答