Exemplo n.º 1
0
def result(start, end, amt) :
    """ Returns string that states starting and ending currencies as well as the final amount """
    amt_dec = Decimal(amt) # This will allow for input to be either float or integer
    end_amt = round(cr.convert(start.upper(), end.upper(), amt_dec), 2) # Conversion function from forex converter
    curr_symbol = cd.get_symbol(end) # Retrieves symbol for ending currency
    response = f"""{curr_symbol}{end_amt}"""
    return response
def get_result(convert_from, convert_to, amount):
    amount = float(amount)
    rates = CurrencyRates(force_decimal=True)
    result_amount = rates.convert(convert_from, convert_to, Decimal(amount))

    symbol = CurrencyCodes().get_symbol(convert_to)

    return f'{symbol} {result_amount:.2f}'
Exemplo n.º 3
0
def code_is_valid(code) :
    """ Returns true if valid currency code and false if not """
    try:
        if not type(code) == str:
            return False
        amt = cr.convert(code.upper(), 'USD', Decimal(20.00))
        return True
    except RatesNotAvailableError:
        return False
Exemplo n.º 4
0
def flash_conversion(firstCurr, secondCurr, amount):
    s = CurrencyCodes()
    c = CurrencyRates()

    symbol = s.get_symbol(secondCurr)
    result = c.convert(firstCurr, secondCurr, Decimal(amount))
    formatted_result = "{:,.2f}".format(result)

    flash_message = f'{symbol} {formatted_result}'
    flash(flash_message, 'success')
Exemplo n.º 5
0
def convert_currency():
    '''
    Converts from one currency to another based on the amount entered

    str(c.convert('USD', 'USD', 1))
    convert from the same currency in order to test the conversions are working
    
    str(c.convert('USD', 'JPY', 100))

    str(c.convert('AAA', 'BBB', 1000))
    this one should create an error
    '''
    cur1 = request.args["cur1"]
    cur2 = request.args["cur2"]
    amount = Decimal(request.args["amount"])
    try: 
        return str(c.convert(cur1, cur2, amount))
    except:
        flash('An error occured')
        return redirect('/')
Exemplo n.º 6
0
def convert():
    """ Display converted amount"""
    if request.method == 'POST':
        # storing three form inputs into variables
        first_currency = request.form.get("convert-from")
        second_currency = request.form.get("convert-to")
        amt = request.form.get("amount")
        # write 3 validation in the form of if statements
        if (first_currency in legit_currencies
                and second_currency in legit_currencies
                and amt.isalpha() is False):
            # if first_currency in legit_currencies
            # if they all pass, this is the HAPPY path
            currency_rate = CurrencyRates(force_decimal=True)
            converted_amount = round(
                currency_rate.convert(first_currency, second_currency,
                                      Decimal(amt)), 2)
            return render_template('converter.html',
                                   converted_amt=converted_amount)
        else:
            return render_template('base.html',
                                   first_currency=first_currency,
                                   second_currency=second_currency,
                                   error=True)
Exemplo n.º 7
0
 def convert(self, cf, ct, amt):
     c = CurrencyRates(force_decimal=True)
     result = c.convert(cf, ct, Decimal(amt))
     return round(result, 2)
Exemplo n.º 8
0
def calc_conversion(convert_from, convert_to, amount):
    symbol = cc.get_symbol(f'{convert_to}')
    conversion = c.convert(f'{convert_from}', f'{convert_to}',
                           Decimal(f'{amount}'))
    return f'{symbol}{round(conversion, 2)}'
Exemplo n.º 9
0
def converter(start, end, initAmt):
    curr_decimal = CurrencyRates(force_decimal=True)
    amt_dec = Decimal(initAmt)
    result = round(curr_decimal.convert(start, end, amt_dec), 2)
    return result