NSNumberFormatter数字From String返回0表示货币样式格式

时间:2011-11-22 22:54:36

标签: ios cocoa-touch ios4 currency nsformatter

我尝试测试以下代码,以便从货币格式的UITextField获取double值(例如:$ 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时,我得到完全相同的字符串,但是当我将char与char进行比较时,它表示$后面的空格是文本字段上的“真实空间”,初始字符串上的differente符号(amountString是我在初始帖子中尝试测试的那个...)。编码问题?

3 个答案:

答案 0 :(得分:0)

代码对我来说是正确的,Xcode同意我的看法。我添加了两行你缺少的内容:

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

并且输出正确。我建议你检查一下如何记录这些值。

答案 1 :(得分:0)

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

答案 2 :(得分: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
   }
}