我的应用使用多种货币,这些货币使用不同的格式,例如:
卢布 的价格显示为:1,101 Руб。
相同金额的美元显示为:US $1 101
如何通过为不同的货币定义一组不同的格式来更改分组分隔符、货币符号和货币符号的位置。
这就是我的短代码的样子
var formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = NSLocale.currentLocale()
formatter.stringFromNumber(4500000)
//Output : $4,500,000.00
//Expected : 4,500,000 Руб.
最佳答案
Swift 4 或更高版本
extension Formatter {
static let belarusianRuble: NumberFormatter = {
let formatter = NumberFormatter()
// set the numberStyle to .CurrencyStyle
formatter.numberStyle = .currency
// set the desired negative and positive formats grouping, and currency symbol position
formatter.positiveFormat = "#,##0 ¤"
formatter.negativeFormat = "-#,##0 ¤"
// set your custom currency symbol
formatter.currencySymbol = "Руб"
return formatter
}()
}
let stringToDisplay = Formatter.belarusianRuble.string(for: 4500000) // "4,500,000 Руб"
extension Formatter {
static let currencyBYR: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.positiveFormat = "#,##0 ¤"
formatter.negativeFormat = "-#,##0 ¤"
formatter.currencySymbol = "Руб"
return formatter
}()
static let currencyEUR: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "pt_PT")
formatter.numberStyle = .currency
return formatter
}()
static let currencyUSD: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.numberStyle = .currency
return formatter
}()
static let currencyBRL: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "pt_BR")
formatter.numberStyle = .currency
return formatter
}()
static let currencyRUB: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "ru_RU")
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 0
return formatter
}()
static let currencyLocale: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = .current
formatter.numberStyle = .currency
return formatter
}()
}
extension Numeric {
var currencyLocale: String { return Formatter.currencyLocale.string(for: self) ?? "" }
var currencyUSD: String { return Formatter.currencyUSD.string(for: self) ?? "" }
var currencyEUR: String { return Formatter.currencyEUR.string(for: self) ?? "" }
var currencyBYR: String { return Formatter.currencyBYR.string(for: self) ?? "" }
var currencyBRL: String { return Formatter.currencyBRL.string(for: self) ?? "" }
var currencyRUB: String { return Formatter.currencyRUB.string(for: self) ?? "" }
}
用法
let amount = 4500000.0
let stringLocale = amount.currencyLocale // "$4,500,000.00"
let stringUSD = amount.currencyUSD // "$4,500,000.00"
let stringEUR = amount.currencyEUR // "4 500 000,00 €"
let stringBRL = amount.currencyBRL // "R$ 4.500.000,00"
let stringBYR = amount.currencyBYR // "4,500,000 Руб"
let stringRUB = amount.currencyRUB // "4 500 000 ₽"
关于ios - 在 Swift 中更改多种货币的分组分隔符、货币符号和货币符号的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34832531/