Converting a String to USD
... 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 $.