在Swift中获取当地货币

bfnvny8b  于 2023-05-05  发布在  Swift
关注(0)|答案(3)|浏览(276)

我正在为多个国家建立一个小的应用程序购买商店。我怎么知道我必须显示美元,欧元等价格...我认为这与localeIdentifier有关,但我不确定如何处理这一点

0dxa2lsx

0dxa2lsx1#

您可以使用以下命令从(NS)Locale获取货币符号和代码

Swift 1和2

let locale = NSLocale.currentLocale()
let currencySymbol = locale.objectForKey(NSLocaleCurrencySymbol)!
let currencyCode = locale.objectForKey(NSLocaleCurrencyCode)!

*Swift 3酒店,纽约

let locale = Locale.current()
let currencySymbol = locale.object(forKey: .currencySymbol)!
let currencyCode = locale.object(forKey: .currencyCode)!

Swift 3.1+

let locale = Locale.current
let currencySymbol = locale.currencySymbol!
let currencyCode = locale.currencyCode!

这与用户区域格式设置相关,在iOSmacOS中均适用

h9vpoimq

h9vpoimq2#

关于Swift3

//User region setting return
let locale = Locale.current //NSLocale.current

//Returns true if the locale uses the metric system (Note: Only three countries do not use the metric system: the US, Liberia and Myanmar.)
let isMetric = locale.usesMetricSystem

//Returns the currency code of the locale. For example, for “zh-Hant-HK”, returns “HKD”.  
let currencyCode  = locale.currencyCode

//Returns the currency symbol of the locale. For example, for “zh-Hant-HK”, returns “HK$”.
let currencySymbol = locale.currencySymbol
2q5ifsrm

2q5ifsrm3#

适用于iOS 16+

//Returns the user's current locale
let locale = Locale.current

//Returns the local currency's symbol
//Ex: $
let currencySymbol = locale.currencySymbol!

//Returns the currency code of the locale. Returns nil if the data isn't available
//Ex: USD
let currencyCode = locale.currency!.identifier

相关问题