Swift playground - How to convert a comma string to a decimal string

I am new to Swift.

How to convert String semicolon to String with decimal point?

The code works fine with a dot (.)

The problem is that I am using a comma (,) ... with: var price

The source of the problem is the decimal French keyboard, using a comma (,) instead of a period (.)

Not sure how to use NSNumberFormatter or generate DecimalNumbers if that is the key. Several options are probed there.

//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) 

Thanks for your time and your help!

+8
source share
4 answers

update: Xcode 8.2.1 • Swift 3.0.2. You can use NumberFormatter () to convert your string to a number. You just need to specify decimalSeparator as follows:

 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" 

Currency formatting should also be done using NumberFormat, so create a read-only computed currency that extends the FloatingPoint protocol to return a formatted string from the String doubleValue property.

 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 // "$2.25" 

 Formatter.currency.locale = Locale(identifier: "en_US") "2222.25".doubleValue.currency // "$2,222.25" "2222,25".doubleValue.currency // "$2,222.25" Formatter.currency.locale = Locale(identifier: "pt_BR") "2222.25".doubleValue.currency // "R$2.222,25" "2222,25".doubleValue.currency // "R$2.222,25" 
+27
source
 var price = "2,25" price = price.replacingOccurrences(of: ",", with: ".") var priceFloat = (price as NSString).floatValue 
+6
source

You can use Swift 3 for this:

 let currentAmount = 2,50 currentAmount = currentAmount.replacingOccurrences(of: ",", with: ".") print(currentAmount) // 2.50 
+3
source

Or if you don't like the extensions:

  let price = "2,25" var counter: Int = 0 var noCommaNumber: String! for var Character in price.characters { if Character == "," { Character = "." } if counter != 0 { noCommaNumber = "\(noCommaNumber)" + "\(Character)" } else { noCommaNumber = "\(Character)" } counter++ } importo = Float(noCommaNumber) let costString = String(format:"%.2f", importo) 
-2
source

Source: https://habr.com/ru/post/981954/


All Articles