Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift iOS - Convert currency formatted string to number

I've converted a number into a currency formatted string (e.g. 2000 -> £2,000.00) using the NumberFormatter class. However I need a way of converting the formatted string back into a number (e.g. £2,000.00 -> 2000).

It turns out simply running it back through the formatter doesn't work and just produces nil. Does anyone know the best way to achieve this?

like image 476
Welton122 Avatar asked Mar 10 '23 21:03

Welton122


1 Answers

Swift 4: The following String extension helps to convert currency formatted strings to decimal or double. The method automatically identifies the number format for most common currencies.

The String has to be without currency symbol (€, $, £, ...):

For Example:

US formatted string: "11.233.39" -> 11233.39

European formatted string: "11.233,39" -> 11233.39

// String+DecimalOrDouble

extension String {   
    func toDecimalWithAutoLocale() -> Decimal? {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal

        //** US,CAD,GBP formatted
        formatter.locale = Locale(identifier: "en_US")

        if let number = formatter.number(from: self) {
            return number.decimalValue
        }
        
        //** EUR formatted
        formatter.locale = Locale(identifier: "de_DE")

        if let number = formatter.number(from: self) {
           return number.decimalValue
        }
        
        return nil
    }
    
    func toDoubleWithAutoLocale() -> Double? {
        guard let decimal = self.toDecimalWithAutoLocale() else {
            return nil
        }

        return NSDecimalNumber(decimal:decimal).doubleValue
    }
}

Example tested in Playground:

import Foundation

//US formatted without decimal mark
str = "11233.3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3

//EU formatted without decimal mark
str = "11233,3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3

//US formatted with decimal mark
str = "11,233.3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3

//EU formatted with decimal mark
str = "11.233,3"
decimal = str.toDecimalWithAutoLocale() //11233.3
double = str.toDoubleWithAutoLocale() //11233.3
like image 125
Peter Kreinz Avatar answered Mar 19 '23 15:03

Peter Kreinz