货币符号和值之间的间距:iOS swift 3
Spacing between currency symbol and value: iOS swift 3
我有一个带有 IAP 的应用程序。我需要根据位置显示带有值的价格。我做到了这一点。现在我可以根据位置显示价格了。
但是,我需要在货币符号和值之间留一些间距。
即,我需要 9.99 美元(目前显示为 9.99 美元)。
仅供参考:我正在从 iTunes 获取价格(使用变量获取从 iTunes 获取的价格并将其显示在文本视图中)。
我没有成功:
我也提到过这个,但效果不佳:
有什么建议吗?
我正在这样做:
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]
有趣的是,对于 subchargelabel 值,我得到了一点间距
在我尝试 运行 该应用程序时,评论部分未被评论。只是想让你知道
感谢您的帮助!
尝试按如下格式设置所需字符串的格式。
let priceWithSpace: String? = firstProduct.priceLocale.currencySymbol + " " + product.price
只输入符号 space 和数量不是一个好主意。它的顺序可能因区域设置或货币而异。下面的方法会给你答案。
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
}
}
如下使用,
if let formattedAmount = customFormattedAmountString(amount: 5000000) {
print(formattedAmount)
}
这将打印出美元,
$ 5,000,000.00
和欧元,
5.000.000,00 €
正如您希望在货币符号和金额之间添加 space。如果您只是像第一个答案一样附加字符串,您将得到“€ 5.000.000,00”,而不是所需的 - “5.000.000,00 €”。
将 formatWidth 属性 和 paddingPosition 设置为 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"
}
我有一个带有 IAP 的应用程序。我需要根据位置显示带有值的价格。我做到了这一点。现在我可以根据位置显示价格了。
但是,我需要在货币符号和值之间留一些间距。 即,我需要 9.99 美元(目前显示为 9.99 美元)。
仅供参考:我正在从 iTunes 获取价格(使用变量获取从 iTunes 获取的价格并将其显示在文本视图中)。
我没有成功:
我也提到过这个,但效果不佳:
有什么建议吗?
我正在这样做:
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]
有趣的是,对于 subchargelabel 值,我得到了一点间距
在我尝试 运行 该应用程序时,评论部分未被评论。只是想让你知道
感谢您的帮助!
尝试按如下格式设置所需字符串的格式。
let priceWithSpace: String? = firstProduct.priceLocale.currencySymbol + " " + product.price
只输入符号 space 和数量不是一个好主意。它的顺序可能因区域设置或货币而异。下面的方法会给你答案。
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
}
}
如下使用,
if let formattedAmount = customFormattedAmountString(amount: 5000000) {
print(formattedAmount)
}
这将打印出美元,
$ 5,000,000.00
和欧元,
5.000.000,00 €
正如您希望在货币符号和金额之间添加 space。如果您只是像第一个答案一样附加字符串,您将得到“€ 5.000.000,00”,而不是所需的 - “5.000.000,00 €”。
将 formatWidth 属性 和 paddingPosition 设置为 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"
}