Swift playground - 如何将带逗号的字符串转换为带小数的字符串
Swift playground - How to convert a string with comma to a string with decimal
我是 Swift 世界的新人。
如何将带逗号的字符串转换为带小数的字符串?
带点 (.) 的代码工作正常
问题是当我使用逗号 (,) ... 时:var price
问题的根源是十进制法语键盘使用逗号 (,) 而不是点 (.)
不知道如何使用 NSNumberFormatter 或 generatesDecimalNumbers 如果它是关键。可能不止一种选择。
//The answer change if "2,25" or "2.25" is used.
var price : String = "2,25"
var priceFloat = (price as NSString).floatValue
//I need to have 2.25 as answer.
var costString = String(format:"%.2f", priceFloat)
感谢您的宝贵时间和帮助!
更新:Xcode 8.2.1 • Swift 3.0.2
您可以使用 NumberFormatter() 将字符串转换为数字。您只需要指定 decimalSeparator 如下:
extension String {
static let numberFormatter = NumberFormatter()
var doubleValue: Double {
String.numberFormatter.decimalSeparator = "."
if let result = String.numberFormatter.number(from: self) {
return result.doubleValue
} else {
String.numberFormatter.decimalSeparator = ","
if let result = String.numberFormatter.number(from: self) {
return result.doubleValue
}
}
return 0
}
}
"2.25".doubleValue // 2.25
"2,25".doubleValue // 2.25
let price = "2,25"
let costString = String(format:"%.2f", price.doubleValue) // "2.25"
您还应该使用 NumberFormat 进行货币格式化,因此创建一个只读计算 属性 货币,将 FloatingPoint 协议扩展到 return 来自 String doubleValue 属性 的格式化字符串。
extension NumberFormatter {
convenience init(style: Style) {
self.init()
self.numberStyle = style
}
}
extension Formatter {
static let currency = NumberFormatter(style: .currency)
}
extension FloatingPoint {
var currency: String {
return Formatter.currency.string(for: self) ?? ""
}
}
let costString = "2,25".doubleValue.currency // ".25"
Formatter.currency.locale = Locale(identifier: "en_US")
"2222.25".doubleValue.currency // ",222.25"
"2222,25".doubleValue.currency // ",222.25"
Formatter.currency.locale = Locale(identifier: "pt_BR")
"2222.25".doubleValue.currency // "R.222,25"
"2222,25".doubleValue.currency // "R.222,25"
编辑:已更新以与 Swift 的当前版本一起使用:
let amount = "8,35"
var counter: Int = 0
var noCommaNumber: String!
for var carattere in (amount) {
if carattere == "," { carattere = "." }
if counter != 0 { noCommaNumber = "\(noCommaNumber ?? "\(carattere)")" + "\(carattere)" } else { noCommaNumber = "\(carattere)" } // otherwise first record will always be nil
counter += 1
}
let importo = Float(noCommaNumber)
var price = "2,25"
price = price.replacingOccurrences(of: ",", with: ".")
var priceFloat = (price as NSString).floatValue
你可以使用 Swift 3:
let currentAmount = "2,50"
currentAmount = currentAmount.replacingOccurrences(of: ",", with: ".")
print(currentAmount) // "2.50\n"
可为空的扩展版本:
extension String
{
static let customNumberFormatter = NumberFormatter()
var doubleValue: Double? {
String.customNumberFormatter.decimalSeparator = "."
if let result = String.customNumberFormatter.number(from: self) {
return result.doubleValue
} else {
String.customNumberFormatter.decimalSeparator = ","
if let result = String.customNumberFormatter.number(from: self) {
return result.doubleValue
}
}
return nil
}
}
我是 Swift 世界的新人。
如何将带逗号的字符串转换为带小数的字符串?
带点 (.) 的代码工作正常
问题是当我使用逗号 (,) ... 时:var price
问题的根源是十进制法语键盘使用逗号 (,) 而不是点 (.)
不知道如何使用 NSNumberFormatter 或 generatesDecimalNumbers 如果它是关键。可能不止一种选择。
//The answer change if "2,25" or "2.25" is used.
var price : String = "2,25"
var priceFloat = (price as NSString).floatValue
//I need to have 2.25 as answer.
var costString = String(format:"%.2f", priceFloat)
感谢您的宝贵时间和帮助!
更新:Xcode 8.2.1 • Swift 3.0.2 您可以使用 NumberFormatter() 将字符串转换为数字。您只需要指定 decimalSeparator 如下:
extension String {
static let numberFormatter = NumberFormatter()
var doubleValue: Double {
String.numberFormatter.decimalSeparator = "."
if let result = String.numberFormatter.number(from: self) {
return result.doubleValue
} else {
String.numberFormatter.decimalSeparator = ","
if let result = String.numberFormatter.number(from: self) {
return result.doubleValue
}
}
return 0
}
}
"2.25".doubleValue // 2.25
"2,25".doubleValue // 2.25
let price = "2,25"
let costString = String(format:"%.2f", price.doubleValue) // "2.25"
您还应该使用 NumberFormat 进行货币格式化,因此创建一个只读计算 属性 货币,将 FloatingPoint 协议扩展到 return 来自 String doubleValue 属性 的格式化字符串。
extension NumberFormatter {
convenience init(style: Style) {
self.init()
self.numberStyle = style
}
}
extension Formatter {
static let currency = NumberFormatter(style: .currency)
}
extension FloatingPoint {
var currency: String {
return Formatter.currency.string(for: self) ?? ""
}
}
let costString = "2,25".doubleValue.currency // ".25"
Formatter.currency.locale = Locale(identifier: "en_US")
"2222.25".doubleValue.currency // ",222.25"
"2222,25".doubleValue.currency // ",222.25"
Formatter.currency.locale = Locale(identifier: "pt_BR")
"2222.25".doubleValue.currency // "R.222,25"
"2222,25".doubleValue.currency // "R.222,25"
编辑:已更新以与 Swift 的当前版本一起使用:
let amount = "8,35"
var counter: Int = 0
var noCommaNumber: String!
for var carattere in (amount) {
if carattere == "," { carattere = "." }
if counter != 0 { noCommaNumber = "\(noCommaNumber ?? "\(carattere)")" + "\(carattere)" } else { noCommaNumber = "\(carattere)" } // otherwise first record will always be nil
counter += 1
}
let importo = Float(noCommaNumber)
var price = "2,25"
price = price.replacingOccurrences(of: ",", with: ".")
var priceFloat = (price as NSString).floatValue
你可以使用 Swift 3:
let currentAmount = "2,50"
currentAmount = currentAmount.replacingOccurrences(of: ",", with: ".")
print(currentAmount) // "2.50\n"
可为空的扩展版本:
extension String
{
static let customNumberFormatter = NumberFormatter()
var doubleValue: Double? {
String.customNumberFormatter.decimalSeparator = "."
if let result = String.customNumberFormatter.number(from: self) {
return result.doubleValue
} else {
String.customNumberFormatter.decimalSeparator = ","
if let result = String.customNumberFormatter.number(from: self) {
return result.doubleValue
}
}
return nil
}
}