I would like to store the Locale to Core Data that I can assign it against a list item. Thus when a user changes a locale the formatting of the original currency etc. is not impacted by the change and thus the app still functions.
I think this would be something useful, even if a user would not typically change the region often.
I have defined a currency formatter using current locale settings:
var currencyFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.usesGroupingSeparator = true
formatter.numberStyle = .currency
formatter.locale = Locale.current
return formatter
}()
I have a function that uses this to a. format a currency and b. strip it for processing:
func formatCurrency(text: String, edit: Bool, currencyCode: String) -> String {
if(!edit && text.count > 0) {
let myDouble = (text as NSString).doubleValue
let myNumber = NSNumber(value:myDouble)
let priceString = currencyFormatter.string(from: myNumber) ?? "0.00"
print(priceString)
return priceString
} else {
let myDouble = (text as NSString).doubleValue
let myNumber = NSNumber(value:myDouble)
let returnValue = currencyFormatter.number(from: text) ?? myNumber
if(returnValue == 0){
return ""
}
return returnValue.stringValue
}
}
I have tried to save the 'currencyFormatter.currencyCode` in Core Data as a text, this works, however since you need to consider symbols for decimal, and separators etc. this becomes clumsy as there are lots of parameters. Below are a few:
print("currencyDecimalSeparator - " + currencyFormatter.currencyDecimalSeparator)
print("currencySymbol - " + currencyFormatter.currencySymbol)
print("currencyGroupingSeparator - " + currencyFormatter.currencyGroupingSeparator)
print("internationalCurrencySymbol - " + currencyFormatter.internationalCurrencySymbol)
print("currencyCode - " + currencyFormatter.currencyCode)
print("decimalSeparator - " + currencyFormatter.decimalSeparator)
I would like to have a neat way of storing Locale.current
that I can assign it back when accessing a specific list item.
Thank you,
Etienne
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…