you must use NSNumberFormatter with product values for price and priceLocale to output a string that is formatted correctly regardless of the user's location. product.price returns the price in local currency as NSDecimalNumber, and product.productLocale returns NSLocale for the price value.
eg,
var item = SKProduct() for i in productsArray { if i.productIdentifier == "com.Company.App.item1" { item = i if let formattedPrice = priceStringForProduct(item) {
where priceStringForProduct is a function defined elsewhere: -
func priceStringForProduct(item: SKProduct) -> String? { let numberFormatter = NSNumberFormatter() let price = item.price let locale = item.priceLocale numberFormatter.numberStyle = .CurrencyStyle numberFormatter.locale = locale return numberFormatter.stringFromNumber(price) }
You can also handle a special case when the price is 0.0 (free level). In this case, change the priceStringForProduct function to:
func priceStringForProduct(item: SKProduct) -> String? { let price = item.price if price == NSDecimalNumber(float: 0.0) { return "GET" //or whatever you like really... maybe 'Free' } else { let numberFormatter = NSNumberFormatter() let locale = item.priceLocale numberFormatter.numberStyle = .CurrencyStyle numberFormatter.locale = locale return numberFormatter.stringFromNumber(price) } }
Edit: add other things when you specify your product array in a more “faster” way:
var productsArray = [SKProduct]()
and then in your didRecieveResponse, instead of iterating over products, you can simply specify productsArray as response.products
var productsArray = [SKProduct]() if response.products.count != 0 { print("\(response.products.map {p -> String in return p.localizedTitle})") productsArray = response.products }
Edit: to test several different locales, I usually make an array of NSLocales, and then print the result cyclically. There is a repo with all the localeIdentifiers here
So:
let testPrice = NSDecimalNumber(float: 1.99) let localeArray = [NSLocale(localeIdentifier: "uz_Latn"), NSLocale(localeIdentifier: "en_BZ"), NSLocale(localeIdentifier: "nyn_UG"), NSLocale(localeIdentifier: "ebu_KE"), NSLocale(localeIdentifier: "en_JM"), NSLocale(localeIdentifier: "en_US")] for locale in localeArray { let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = .CurrencyStyle numberFormatter.locale = locale print(numberFormatter.stringFromNumber(testPrice)) }