没有“$”的Cocoa NSNumberFormatterCurrencyStyle返回零 [英] Cocoa NSNumberFormatterCurrencyStyle without "$" return zero

查看:265
本文介绍了没有“$”的Cocoa NSNumberFormatterCurrencyStyle返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 $ 3.50转换为3.50 
3.50转换为0

以下是转换器的代码:

  // formatter将值转换为货币字符串
NSNumberFormatter * currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setGeneratesDecimalNumbers:YES];




<解决方案

我遇到了类似的问题。我结束了报废NSNumberFormatter解析和切换到RegexKit Lite有更好的结果。



在这里抓取一个副本: RegexKit Lite



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


I have a number formatter set up to convert currency strings to decimal values. The problem is that if the text string does not have a leading dollar sign ("$"), it gets converted to 0, rather than a valid matching number. So:

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

Here is the code for the converter:

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

Am I missing something?

解决方案

I ran into a similar problem. I ended up scrapping the NSNumberFormatter for parsing and switched over to RegexKit Lite with much better results.

Grab a copy here: RegexKit Lite

Then go here http://regexlib.com/ and find the regex that works best for you.

这篇关于没有“$”的Cocoa NSNumberFormatterCurrencyStyle返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆