【问题标题】:Cocoa NSNumberFormatterCurrencyStyle without "$" return zero不带“$”的 Cocoa NSNumberFormatterCurrencyStyle 返回零
【发布时间】:2009-07-20 22:35:23
【问题描述】:

我设置了一个数字格式化程序,用于将货币字符串转换为十进制值。问题是如果文本字符串没有前导美元符号(“$”),它会被转换为 0,而不是有效的匹配数字。所以:

"$3.50" converts to 3.50
 "3.50" converts to 0

这是转换器的代码:

// formatter to convert a value to and from a currency string
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setGeneratesDecimalNumbers:YES];

我错过了什么吗?

【问题讨论】:

    标签: cocoa numbers currency formatter


    【解决方案1】:

    我遇到了类似的问题。我最终放弃了 NSNumberFormatter 进行解析并切换到 RegexKit Lite,结果更好。

    在此处获取副本:RegexKit Lite

    然后去这里http://regexlib.com/ 并找到最适合您的正则表达式。

    【讨论】:

      【解决方案2】:

      它将“3.50”转换为 0,因为它无法将“3.50”识别为此处提到的有效货币字符串:http://blog.clickablebliss.com/2006/10/05/the-challenges-of-international-money-in-cocoa/。如果默认的 NSNumberFormatterCurrencyStyle 不够灵活,无法满足您的需求,您可能需要将其子类化以处理这些情况。

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        • 1970-01-01
        • 2020-11-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        相关资源
        最近更新 更多