2009-07-20 34 views
3

我有一個數字格式化程序設置爲貨幣字符串轉換爲十進制值。問題是,如果文本字符串沒有前導美元符號(「$」),它將被轉換爲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]; 

我缺少的東西?

回答

1

我遇到了類似的問題。我最終取消了NSNumberFormatter進行解析,並切換到RegexKit Lite,並獲得了更好的結果。

搶複製點擊這裏:RegexKit Lite

然後去這裏http://regexlib.com/,找到最適合你的正則表達式。