Converting a String to USD I'm working on a clientside iOS app for my school that involves students purchasing things, and I wrote a String extension for formatting a String into USD. Is there a way to improve this or make it more "swifty"?

extension String {
    var usdFormat: String {
        var str = self
        var postDec = str.components(separatedBy: ".")
        switch postDec[1].count {
        case 0: str.append("00")
        case 1: str.append("0")
        default: break
        }

        str = "$" + str

        return str
    }
}
... for formatting a String into USD ...


A string is not the appropriate type to hold a monetary amount. What would your
extension method return if called on the string "abc" or "?" ? A price is a number
and should be stored as such. Possible choices are


An integer holding the price in units of the smallest denomination (e.g. cents), or
A Decimal which represents a base-10 number with up do 38 decimal digits.


Creating a textual representation of a number is the job for a 
NumberFormatter.
It has options to specify the desired number of fractional digits, and even
has a dedicated .currency
style.

Using a number formatter has several advantages over “manually” formatting the price,
such as:


It chooses the appropriate style according to the user's locale.
It puts the currency symbol at the correct position.
It rounds the number to the correct number of decimal places.


Formatting a US dollar currency value is as simple as

let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = "USD"

let usPrice: Decimal = 1234.56
let displayString = formatter.string(for: usPrice)!
print(displayString)


This produces the string $1,234.56 if the user's locale is "en_US".
On my device (with a german locale) it produces 1.234,56 $.