Reputation: 481
I am having an app with IAP. I need to show the prices with values based on location. I have made till this. Now I am able to display the prices based on location.
However, I need some spacing between currency symbol and the value. i.e, I need $ 9.99 (currently it shows $9.99).
FYI: I am getting the price from itunes (used a variable to get the price got from itunes and displaying it in a textview).
I have followed this without success: NSNumberFormatter paddingPosition doesn't work
I have also referred this, but it does not work as well: Swift 3 and NumberFormatter (.currency) == ¤?
Any suggestions?
I am doing this:
var index: Int = 0 {
didSet {
var monthlyValue = String()
if prefs.value(forKey: "monthly") != nil {
monthlyValue = prefs.value(forKey: "monthly") as! String
}
let subcharges : Array<String> = ["Free but limited just to get started","or \(monthlyValue) Billed Monthly",""]
let yearlypermonth = Int()
var mycurrency1 = String()
if prefs.value(forKey: "yearly") != nil {
let yearlyvalue = prefs.value(forKey: "yearly") as! String
print("wallet view yearlyvalue: \(yearlyvalue)")
let firstProduct = StoreManager.shared.productsFromStore[0]
if let mycurrency = firstProduct.priceLocale.currencySymbol {
print("mycurrency: \(mycurrency)")
mycurrency1 = mycurrency
prefs.set(mycurrency, forKey: "mycurrency")
}
let formatter = NumberFormatter()
formatter.numberStyle = .currency
// formatter.paddingPosition = .afterPrefix
//
// //formatter.locale = Locale(identifier: "en_US")
// formatter.paddingCharacter = " "
// formatter.formatWidth = 7
let number1 = formatter.number(from: yearlyvalue)
print("wallet view number1: \(number1)")
let secondProd = StoreManager.shared.productsFromStore[1]
let numberFormatter = NumberFormatter()
// numberFormatter.paddingPosition = .afterPrefix
// numberFormatter.paddingCharacter = " "
// numberFormatter.formatWidth = 7
// Get its price from iTunes Connect
numberFormatter.locale = secondProd.priceLocale
let price2Str = numberFormatter.string(from: secondProd.price)
let currencynumbers2 = secondProd.price.doubleValue / 12
monthonValueforaYear = String(format: "%.2f", currencynumbers2)
print("walletview VC monthonValueforaYear: \(monthonValueforaYear)")
//prefs.set(price2Str, forKey: "yearly")
prefs.set(monthonValueforaYear, forKey: "monthonValueforaYear")
}
var charges : Array<String> = ["\(mycurrency1) 0/m","\(mycurrency1 + monthonValueforaYear)/m","Coming Soon"]
print("wallet view charges: \(charges)")
self.Headingtitle.text = Headingarray[index]
self.planchargelabel.text = charges[index]
self.subchargelabel.text = subcharges[index]
Interestingly, for subchargelabel value, I get a little spacing
The commented section was uncommented while I tried running the app. Just so you know it
Thanks for your help!
Upvotes: 0
Views: 2962
Reputation: 2834
set the formatWidth property and paddingPosition to afterPrefix
var formatCurrency: String {
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .currency
formatter.locale = Locale.current
formatter.paddingPosition = .afterPrefix
formatter.formatWidth = 6
return formatter.string(for: self) ?? "N/A"
}
Upvotes: 0
Reputation: 792
Just putting symbol, space, and amount is not a good idea. It's sequence may differ by locale or currency. Following method will give you an answer.
func customFormattedAmountString(amount: Int) -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale.current
if let amountString = formatter.string(from: NSNumber(value: amount)) {
// check if it has default space like EUR
let hasSpace = amountString.rangeOfCharacter(from: .whitespaces) != nil
if let indexOfSymbol = amountString.characters.index(of: Character(formatter.currencySymbol)) {
if amountString.startIndex == indexOfSymbol {
formatter.paddingPosition = .afterPrefix
} else {
formatter.paddingPosition = .beforeSuffix
}
}
if !hasSpace {
formatter.formatWidth = amountString.characters.count + 1
formatter.paddingCharacter = " "
}
} else {
print("Error while making amount string from given amount: \(amount)")
return nil
}
if let finalAmountString = formatter.string(from: NSNumber(value: amount)) {
return finalAmountString
} else {
return nil
}
}
Use it as below,
if let formattedAmount = customFormattedAmountString(amount: 5000000) {
print(formattedAmount)
}
That will print out dollar,
$ 5,000,000.00
and Euro,
5.000.000,00 €
As you wanted to have space between currency symbol and amount. If you just append string like first answer, you'll get "€ 5.000.000,00", not a desired one - "5.000.000,00 €".
Upvotes: 1
Reputation: 8322
Try to format your required string as below.
let priceWithSpace: String? = firstProduct.priceLocale.currencySymbol + " " + product.price
Upvotes: 1