Created
October 27, 2021 19:01
-
-
Save dankkom/9c0978ad91ddd5572786d1f25ce630be to your computer and use it in GitHub Desktop.
Funções para convertes moedas brasileiras antigas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def convert_brl_date(value: float, date: datetime.date) -> float: | |
"""Convert brazillian currency to Real (BRL), given the date of the value. | |
Works with dates from 1942-11-01 to today. | |
""" | |
if date >= datetime.date(1994, 7, 1): # R$ | |
return value | |
elif date >= datetime.date(1993, 8, 1): # CR$ | |
return value / 2_750 | |
elif date >= datetime.date(1990, 3, 16): # Cr$ | |
return value / 2_750_000 | |
elif date >= datetime.date(1989, 1, 16): # NCz$ | |
return value / 2_750_000 | |
elif date >= datetime.date(1986, 2, 28): # Cz$ | |
return value / 2_750_000_000 | |
elif date >= datetime.date(1970, 5, 15): # Cr$ | |
return value / 2_750_000_000_000 | |
elif date >= datetime.date(1967, 2, 13): # NCr$ | |
return value / 2_750_000_000_000 | |
elif date >= datetime.date(1942, 11, 1): # Cr$ | |
return value / 2_750_000_000_000_000 | |
else: | |
raise ValueError("Date out of range (1942-11-01 => today)") | |
def convert_brl_currency_symbol(value: float, currency: str) -> float: | |
"""Convert brazillian currency to Real (BRL), given the currency symbol.""" | |
if currency == "R$": | |
return value | |
elif currency == "CR$": | |
return value / 2_750 | |
elif currency == "Cr$": | |
return value / 2_750_000 | |
elif currency == "NCz$": | |
return value / 2_750_000 | |
elif currency == "Cz$": | |
return value / 2_750_000_000 | |
else: | |
raise ValueError(f"Currency not recognized ({currency})") | |
def convert_brl_currency_name(value: float, currency: str) -> float: | |
"""Convert brazillian currency to Real (BRL), given the currency name.""" | |
currency = currency.lower() | |
if currency == "reais" or currency == "real": | |
return value | |
elif currency == "cruzeiros reais" or currency == "cruzeiro real": | |
return value / 2_750 | |
elif currency == "cruzeiros" or currency == "cruzeiro": | |
return value / 2_750_000 | |
elif currency == "cruzados novos" or currency == "cruzado novo": | |
return value / 2_750_000 | |
elif currency == "cruzados" or currency == "cruzado": | |
return value / 2_750_000_000 | |
else: | |
raise ValueError(f"Currency not recognized ({currency})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment