使用NSNumberFormatter从国际货币字符串获取十进制值 [英] Using NSNumberFormatter to get a decimal value from an international currency string

查看:134
本文介绍了使用NSNumberFormatter从国际货币字符串获取十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎NSNumberFormatter不能将欧元(和可能其他)货币字符串解析为数字类型。有人可以证明我错了。

It seems that the NSNumberFormatter can't parse Euro (and probably other) currency strings into a numerical type. Can someone please prove me wrong.

我尝试使用以下方式从货币字符串中获取数字金额:

I'm attempting to use the following to get a numeric amount from a currency string:

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *currencyNumber = [currencyFormatter numberFromString:currencyString];

这适用于英国和美国的货币金额。它甚至会处理$,£和千位分隔符没有问题。

This works fine for UK and US currency amounts. It even deals with $ and £ and thousands separators with no problems.

但是,当我使用它与欧元货币金额(区域格式设置为法国或德国设置app),它返回一个空字符串。所有以下字符串都会失败:

However, when I use it with euro currency amounts (with the Region Format set to France or Germany in the settings app) it returns an empty string. All of the following strings fail:

12,34 €
12,34
12.345,67 €
12.345,67

值得注意的是,这些字符串与NSNumberFormatter

It's worth noting that these strings match exactly what comes out of the NSNumberFormatter's stringFromNumber method when using the corresponding locale.

在设置应用程序中将Region Format设置为France,然后在下面的代码中将currencyNumber设置为12.34,会导致currencyString设置为'12,34€':

Setting the Region Format to France in the settings app, then setting currencyNumber to 12.34 in the following code, results in currencyString being set to '12,34 €' :

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *currencyString = [currencyFormatter stringFromNumber:currencyNumber];

明显地,这个问题很容易针对欧元来解决这个问题,但我希望在尽可能多的国家/地区销售此应用程序,我认为类似的情况必然发生与其他地区。

It would obviously be fairly easy to hack around this problem specifically for the Euro but I'm hoping to sell this app in as many countries as possible and I'm thinking that a similar situation is bound to occur with other locales.

有人有答案吗?

TIA,Duncan

TIA, Duncan

推荐答案

做以下工作没有问题:

double moneyAmount = 1256.34;
NSLocale *french = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"]; 
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];

[currencyStyle setLocale:french];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *amount = [NSNumber numberWithDouble:moneyAmount];
NSString *amountString =  [currencyStyle stringFromNumber:amount];

NSNumber *pAmount = [currencyStyle numberFromString:amountString]; // == 1256.34

但是, -currencyGroupingSeparator 方法返回字符串 \\\  ,a 不间断空格,而不是预期的

However, the output of the -currencyGroupingSeparator method returns the string \u00a0, a non-breaking space, rather than the expected ,.

OS X / iOS上的类(以及其他正则表达式类)使用 ICU库,也被其他项目使用。我至少找到了两个其他 bug 报告关于这个特定问题。不过,这似乎是预期的行为。 (我花了一些时间通过ICU来源找到这是定义的,但它是一个迷宫。)

The formatter classes (as well as the regex classes, among others) on OS X / iOS make use of the ICU library, also used by other projects. I found at least two other bug reports with regards to this particular issue. It appears that this is expected behaviour, though. (I spent some time sifting through the ICU source to find where this is defined, but it is a labyrinth.)

我的建议,暂时,将是将 -setLenient:YES 传递给货币格式化程序。甚至可以用苹果的雷达报告错误报告。

My recommendation, for the time being, would be to pass -setLenient:YES to the currency formatter. Perhaps even file a bug report with Apple's radar.

这篇关于使用NSNumberFormatter从国际货币字符串获取十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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