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

前端 未结 5 1758
不知归路
不知归路 2020-12-03 17:52

I\'m new in the Swift world.

How can I converting a String with a comma to a String with a decimal?

The code work\'s fine with a dot (.)

The problem

相关标签:
5条回答
  • 2020-12-03 18:29

    You can use for this Swift 3:

    let currentAmount = "2,50"
    
    currentAmount = currentAmount.replacingOccurrences(of: ",", with: ".")
    
    print(currentAmount) // "2.50\n"
    
    0 讨论(0)
  • 2020-12-03 18:30
    var price = "2,25"
    price = price.replacingOccurrences(of: ",", with: ".")
    var priceFloat = (price as NSString).floatValue
    
    0 讨论(0)
  • 2020-12-03 18:30

    EDIT: Updated to work with the current version of 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)
    
    0 讨论(0)
  • 2020-12-03 18:42

    Nullable extension version:

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-03 18:43

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

    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"
    

    You should do the currency formatting also with NumberFormat, so create a read-only computed property currency extending 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"
    
    0 讨论(0)
提交回复
热议问题