Get local currency in swift

后端 未结 2 942
灰色年华
灰色年华 2021-01-03 23:55

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 wit

相关标签:
2条回答
  • 2021-01-04 00:12

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

    Swift 1 and 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!
    

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

    0 讨论(0)
  • 2021-01-04 00:31

    for 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
    
    0 讨论(0)
提交回复
热议问题