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

查看:14
本文介绍了为 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,格式化程序将根据该语言环境自动运行.数字格式化程序的默认语言环境始终是用户选择的区域格式的货币.

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:

NSLocale *aLocale = [[NSLocale alloc] initWithLocaleIdentifier: "nl-NL"]
[currencyFormatter setLocale:aLocale];

将为该语言环境选择默认货币.

Which will choose the default currency for that locale.

虽然您通常不是以用户的当地货币收费,而是以您自己的货币收费.要强制 NSNumberFormatter 以您的货币格式化,同时保持用户偏好的数字格式,请使用:

Often though you're not charging in your user's local currency, but in your own. To force NSNumberFormatter to format in your currency, while keeping the number formatting in the user's preference, use:

currencyFormatter.currencyCode = @"USD"
currencyFormatter.internationalCurrencySymbol = @"$"
currencyFormatter.currencySymbol = @"$"

在 en-US 中,这将格式化为 $5.00 在 nl-NL 中为 $ 5,00.

In en-US this will format as $5.00 in nl-NL it's $ 5,00.

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

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