iPhone的本地化货币 [英] Localize Currency for iPhone

查看:232
本文介绍了iPhone的本地化货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的iPhone应用程序允许使用适当的符号($,€,₤,¥等)为用户输入,显示和存储货币金额。

I would like my iPhone app to allow the input, display and storage of currency amounts using the appropriate symbol ($, €, ₤, ¥, etc) for the user.

NSNumberFormatter能做我需要的一切吗?当用户切换其区域设置时,会发生什么,这些金额(美元,日元等)存储为NSDecimalNumbers。我认为,为了安全,有必要以某种方式捕获进入时的区域设置,然后货币符号,并将其与NSDecimalNumber ivar一起存储在我的实例中,以便它们可以被解开并适当地显示在路上,如果用户更改他们的区域设置自创建项目时的时间?

Would NSNumberFormatter do everything I need? What happens when a user switches their locale and these amounts (dollars, yen, etc.) are stored as NSDecimalNumbers. I assume, to be safe, it's necessary to somehow capture the locale at the time of entry and then the currency symbol and store them in my instance along with the NSDecimalNumber ivar so they can be unwrapped and displayed appropriately down the road should the user changed their locale since the time when the item was created?

对不起,我没有本地化经验,所以希望在跳水之前几个快速指针。最后,任何洞察如何你会处理这种输入给定的iPhone的键盘的限制吗?

Sorry, I have little localization experience so hoping for a couple quick pointers before diving in. Lastly, any insight on how to you handle this kind of input given the limitations of the iPhone's keyboards?

推荐答案

NSNumberFormatter是绝对的方式去!您可以在
NSNumberFormatter上设置NSLocale,格式化程序将根据该语言环境自动运行。数字格式化程序的默认语言环境始终是用户选择的区域格式的货币。

NSNumberFormatter is definitely the way to go! You can set a NSLocale on the NSNumberFormatter, the formatter will automatically behave according to that locale. The default locale for a number formatter is always the currency for the users selected region format.

NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:@"5.00"];

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSLog(@"%@", [currencyFormatter stringFromNumber:someAmount]);

这将根据用户默认区域格式记录金额5.00。如果您要更改货币,您可以设置:

This will log the amount '5.00' according to the users default region format. If you want to alter the currency you can set:

[currencyFormatter setLocale:aLocale];

这篇关于iPhone的本地化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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