将 String 转换为 Double 不适用于 Swift 中的更大值
Converting String to Double not working for bigger values in Swift
我只是想将 String
转换为 Double
。这是我为此使用的功能:
private func calculateListPrice(index: Int) -> Double {
var price = Double(0.0)
for wish in self.dataSourceArray[index].wishes {
var priceTrimmed = wish.price.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
priceTrimmed = priceTrimmed.replacingOccurrences(of: ",", with: ".")
print(priceTrimmed)
if let doublePrice = Double(priceTrimmed) {
price += doublePrice
// return value * 100 so updateAmount calculates correct Int Value
}
}
let rounded = Double(round(100*price)/100)
print(rounded)
return rounded
}
问题是这不适用于每个数字。这是一个免费的例子:
no: 999.999.99
yes: 2.22
yes: 505.05
yes: 31.11
no: 3.111.50
yes: 999.99
no: 2.000.00
您可以通过将其样式设置为货币类型来使用数字格式化程序。我已经实现了一个例子如下:-
let formatter = NumberFormatter()
let frenchFormat = Locale(identifier: "fr_FR")
let germanFormat = Locale(identifier: "de_DE")
formatter.numberStyle = .currency
formatter.locale = frenchFormat
if let frenchPriceValue = formatter.number(from: "100,96€"){
print(frenchPriceValue) //Output is:- 100.96
}
或
formatter.locale = germanFormat
if let germanPriceValue = formatter.number(from: "123,33€"){
print(germanPriceValue)//Output is:- 123.33
}
您可能应该使用 NumberFormatter,因为它支持解析货币(就像您正在尝试做的那样),并且比使用手动方法更容易且不太可能出错。
因此,例如,如果您想使用当前用户的语言环境解析货币,您可以使用:
func priceToDouble(price: String) -> Double? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
guard let result = currencyFormatter.number(from: price) else { return nil }
return Double(result)
}
它将根据区域设置解析货币金额,例如,如果用户的区域设置为美国,它会将“$1,234.56”转换为 Double(1234.56)。如果用户区域设置为欧洲,它会将“€2.345,60”转换为 Double(2345.6)
您也可以手动设置区域设置,因此,将 currencyFormatter.locale = Locale.current
替换为 currencyFormatter.locale = Locale(identifier: "eu")
将导致它始终使用欧洲货币格式。
我只是想将 String
转换为 Double
。这是我为此使用的功能:
private func calculateListPrice(index: Int) -> Double {
var price = Double(0.0)
for wish in self.dataSourceArray[index].wishes {
var priceTrimmed = wish.price.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.").inverted)
priceTrimmed = priceTrimmed.replacingOccurrences(of: ",", with: ".")
print(priceTrimmed)
if let doublePrice = Double(priceTrimmed) {
price += doublePrice
// return value * 100 so updateAmount calculates correct Int Value
}
}
let rounded = Double(round(100*price)/100)
print(rounded)
return rounded
}
问题是这不适用于每个数字。这是一个免费的例子:
no: 999.999.99
yes: 2.22
yes: 505.05
yes: 31.11
no: 3.111.50
yes: 999.99
no: 2.000.00
您可以通过将其样式设置为货币类型来使用数字格式化程序。我已经实现了一个例子如下:-
let formatter = NumberFormatter()
let frenchFormat = Locale(identifier: "fr_FR")
let germanFormat = Locale(identifier: "de_DE")
formatter.numberStyle = .currency
formatter.locale = frenchFormat
if let frenchPriceValue = formatter.number(from: "100,96€"){
print(frenchPriceValue) //Output is:- 100.96
}
或
formatter.locale = germanFormat
if let germanPriceValue = formatter.number(from: "123,33€"){
print(germanPriceValue)//Output is:- 123.33
}
您可能应该使用 NumberFormatter,因为它支持解析货币(就像您正在尝试做的那样),并且比使用手动方法更容易且不太可能出错。
因此,例如,如果您想使用当前用户的语言环境解析货币,您可以使用:
func priceToDouble(price: String) -> Double? {
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = Locale.current
guard let result = currencyFormatter.number(from: price) else { return nil }
return Double(result)
}
它将根据区域设置解析货币金额,例如,如果用户的区域设置为美国,它会将“$1,234.56”转换为 Double(1234.56)。如果用户区域设置为欧洲,它会将“€2.345,60”转换为 Double(2345.6)
您也可以手动设置区域设置,因此,将 currencyFormatter.locale = Locale.current
替换为 currencyFormatter.locale = Locale(identifier: "eu")
将导致它始终使用欧洲货币格式。