xcode 快速获取当地货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37929026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:56:37  来源:igfitidea点击:

Get local currency in swift

xcodeswiftlocalecurrency

提问by Andrew B.

I am setting up an little in app purchase store for muliple countries. How can I figure out that I have to show up the price in Dollar, Euro etc... I think it have to do with the localeIdentifier but I am not sure how to handle this

我正在为多个国家/地区设置一些应用程序购买商店。我怎么知道我必须以美元、欧元等显示价格......我认为这与 localeIdentifier 有关,但我不确定如何处理

回答by vadian

You can get the currency symbol and code from (NS)Localewith

你可以从货币符号和代码(NS)Locale

Swift 1 and 2

斯威夫特 1 和 2

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

Swift 3

斯威夫特 3

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

Swift 3.1

斯威夫特 3.1

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

This correlates with the user region format settings and works in both iOSand macOS

这与用户区域格式设置相关,并且适用于iOSmacOS

回答by Nazmul Hasan

for swift3

对于 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