Is it possible to save numbers with grouping separators and currency into core data and if so can someone point me in the right direction.
是否可以通过将分隔符和货币分组到核心数据来保存数字,如果是这样,有人可以指出我正确的方向。
I have mile.text which is a decimal number with the grouping separator, payPerMile.text which is currency and then the grossPay.text which is also currency.
我有mile.text这是一个带分组分隔符的十进制数字,payPerMile.text是货币,然后是grossPay.text,它也是货币。
Everything calculates and displays fine until I save it. I print the numbers when I save, everything after the grouping separator is gone and the currency lines display as NaN.
一切都计算并显示正常,直到我保存。我保存时打印数字,分组分隔符后的所有内容都消失,货币行显示为NaN。
@IBAction func save(_ sender: UIButton) {
if let item = item {
item.startdate = startDate.text
item.unitnumber = unitNumber.text
item.miles = NSDecimalNumber(string: miles.text ?? "0.0")
item.paypermile = NSDecimalNumber(string: payPerMile.text ?? "0.00")
item.grosspay = NSDecimalNumber(string: grossPay.text ?? "0.00")
item.company = company.text
item.destination = destination.text
item.enddate = endDate.text
} else if let entitydescription = NSEntityDescription.entity(forEntityName: "DriveAwayMain", in: pc) {
let item = DriveAwayMain(entity: entitydescription, insertInto: pc)
item.startdate = startDate.text
item.unitnumber = unitNumber.text
item.miles = NSDecimalNumber(string: miles.text ?? "0.0")
item.paypermile = NSDecimalNumber(string: payPerMile.text ?? "0.00")
item.grosspay = NSDecimalNumber(string: grossPay.text ?? "0.00")
item.company = company.text
item.destination = destination.text
item.enddate = endDate.text
}
1 个解决方案
#1
0
NSDecimalNumber(string:)
only accepts plain numbers, not formatted numbers. If the user is entering formatted currency values then you need to use a NumberFormatter
in .currency
mode to parse the string into a number.
NSDecimalNumber(string :)只接受普通数字,而不是格式化数字。如果用户输入格式化的货币值,则需要在.currency模式下使用NumberFormatter将字符串解析为数字。
Example:
print(NSDecimalNumber(string: "$4,560.45"))
Output:
NaN
Using a NumberFormatter
:
使用NumberFormatter:
let curFmt = NumberFormatter()
curFmt.generatesDecimalNumbers = true
curFmt.numberStyle = .currency
if let num = curFmt.number(from: "$4,560.45") {
print(num)
} else {
print("Not valid currency number")
}
Output:
4560.45
#1
0
NSDecimalNumber(string:)
only accepts plain numbers, not formatted numbers. If the user is entering formatted currency values then you need to use a NumberFormatter
in .currency
mode to parse the string into a number.
NSDecimalNumber(string :)只接受普通数字,而不是格式化数字。如果用户输入格式化的货币值,则需要在.currency模式下使用NumberFormatter将字符串解析为数字。
Example:
print(NSDecimalNumber(string: "$4,560.45"))
Output:
NaN
Using a NumberFormatter
:
使用NumberFormatter:
let curFmt = NumberFormatter()
curFmt.generatesDecimalNumbers = true
curFmt.numberStyle = .currency
if let num = curFmt.number(from: "$4,560.45") {
print(num)
} else {
print("Not valid currency number")
}
Output:
4560.45