-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.py
71 lines (56 loc) · 1.81 KB
/
conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
symbols = {"USD": "$", "CAD": "$", "SGD": "$", "NZD": "$", "AUD": "$", "HKD": "$", "MXN": "$",
"BRL": "R$",
"RUB": "₽",
"GBP": "£",
"CNY": "¥", "JPY": "¥",
"EUR": "€",
"KRW": "₩",
"INR": "₹",
"SEK": "kr", "DKK": "kr", "NOK": "kr", "ISK": "kr",
"ILS": "₪",
"PLN": "zł",
"CZK": "Kč",
"IDR": "Rp",
"PHP": "₱",
"HRK": "kn",
"TRY": "₺",
"ZAR": "R",
"CHF": "CHF",
"BGN": "лв",
"HUF": "Ft",
"THB": "฿",
}
def getRate(fromCurrency, toCurrency):
# fetch exchange rate from API
r = requests.get(
url="https://api.exchangeratesapi.io/latest?base="+fromCurrency)
# parse data
data = r.json()
try:
data["rates"][toCurrency]
except:
return None
return data["rates"][toCurrency]
def formatMoney(unformatted):
# make money appear with 2dp
rounded = round(unformatted, 2)
# remove excess decimal places and stringify
money = str(int(rounded * 100))
# if money < 1, add first character of 0, else we get $.xy
if money[:-2] == "":
money = "0" + money
# return value with decimal point
return money[:-2] + "." + money[-2:]
def displayMoney(unformatted, currency):
formattedAmount = formatMoney(unformatted)
return getSymbol(currency) + formattedAmount
def getSymbol(currency):
if type(currency) != str or len(currency) != 3:
raise TypeError("Tried to get symbol for non ISO code")
currency = currency.upper()
# If we've found a symbol, return it, else just use the 3 letter currency code
try:
return symbols[currency]
except:
return currency + " "