gpt4 book ai didi

iphone - 为 iPhone 本地化货币

转载 作者:行者123 更新时间:2023-12-03 18:22:05 31 4
gpt4 key购买 nike

我希望我的 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

关于iphone - 为 iPhone 本地化货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/856019/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com