我希望我的 iPhone 应用程序允许用户使用适当的符号($、€、₤、¥ 等)输入、显示和存储货币金额。
NSNumberFormatter 会做我需要的一切吗?当用户切换其区域设置并且这些金额(美元、日元等)存储为 NSDecimalNumbers 时会发生什么。我认为,为了安全起见,有必要以某种方式捕获输入时的区域设置,然后捕获货币符号,并将它们与 NSDecimalNumber ivar 一起存储在我的实例中,以便在用户更改时可以将它们展开并正确显示自创建项目以来的区域设置?
抱歉,我几乎没有本地化经验,因此希望在开始之前能获得一些快速提示。最后,考虑到 iPhone 键盘的限制,您对如何处理此类输入有什么见解吗?
NSNumberFormatter 绝对是您的最佳选择!您可以在
NSNumberFormatter,格式化程序将自动根据该区域设置进行操作。数字格式化程序的默认区域设置始终是用户选择的区域格式的货币。
NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", [currencyFormatter stringFromNumber:someAmount]);
这将根据用户默认区域格式记录金额“5.00”。如果您想更改货币,您可以设置:
NSLocale *aLocale = [[NSLocale alloc] initWithLocaleIdentifier: "nl-NL"]
[currencyFormatter setLocale:aLocale];
这将选择该区域设置的默认货币。
通常,您不是以用户的本地货币收费,而是以您自己的货币收费。强迫NSNumberFormatter
要以您的货币进行格式化,同时保留用户首选项中的数字格式,请使用:
currencyFormatter.currencyCode = @"USD"
currencyFormatter.internationalCurrencySymbol = @"$"
currencyFormatter.currencySymbol = @"$"
在 en-US 中,格式为$5.00
在 nl-NL 中是$ 5,00
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)