Skip to content

Commit efb8084

Browse files
committed
moving all my practice work to this org
1 parent 74c982e commit efb8084

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

README.playground/Contents.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import UIKit
2+
3+
enum Currency {
4+
case cad
5+
case mxn
6+
}
7+
8+
let usToCad = 1.30
9+
let usToMxn = 20.32
10+
var currency: Currency = .cad
11+
12+
var currencyFormatter: NumberFormatter = {
13+
let formatter = NumberFormatter()
14+
formatter.numberStyle = .currency
15+
return formatter
16+
}()
17+
18+
func convert(_ dollars: Double) -> Double {
19+
var result = 0.0
20+
switch currency {
21+
case .cad:
22+
result = dollars * usToCad
23+
case .mxn:
24+
result = dollars * usToMxn
25+
}
26+
return result
27+
}
28+
29+
func convert(amountString: String) -> String? {
30+
guard let amount = Double(amountString) else { return "Amount string could not be converted" }
31+
let conversionResult = convert(amount)
32+
var resultString = ""
33+
34+
switch currency {
35+
case .cad:
36+
currencyFormatter.currencyCode = "CAD"
37+
currencyFormatter.currencySymbol = "C$"
38+
currencyFormatter.currencyDecimalSeparator = "."
39+
currencyFormatter.currencyGroupingSeparator = ","
40+
currencyFormatter.internationalCurrencySymbol = "C$"
41+
case .mxn:
42+
currencyFormatter.currencyCode = "MXN"
43+
currencyFormatter.currencySymbol = "MX$"
44+
currencyFormatter.currencyDecimalSeparator = "."
45+
currencyFormatter.currencyGroupingSeparator = ","
46+
currencyFormatter.internationalCurrencySymbol = "MX$"
47+
}
48+
49+
if let unwrappedString = currencyFormatter.string(from: conversionResult as NSNumber) {
50+
resultString = unwrappedString
51+
} else {
52+
resultString = "Number formatting failed."
53+
}
54+
55+
return resultString
56+
}
57+
58+
print(convert(100))
59+
if let successfulConversion = convert(amountString: "100") {
60+
print(successfulConversion)
61+
}
62+
63+
currency = .mxn
64+
65+
print(convert(100))
66+
if let successfulConversion = convert(amountString: "100") {
67+
print(successfulConversion)
68+
}
69+
70+
71+
72+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

README.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)