ghovat
ghovat

Reputation: 1043

Curency Rate Conversion Wrong Base Currency

I need a currency rate update script, but I have the following problem If I call the interface, I get the following response:

"base": "USD",
"rates": {
"AED": 3.672782,
"AFN": 57.680075,
"ALL": 130.5485,
"AMD": 477.866,
"ANG": 1.78952,
"EUR": 0.927815,
"AOA": 107.13525,

...

The Problem is, I need the currency rate conversion in EUR. That means, my base rate is USD, but I need it in EUR.

Is there any option to calculate from this JSON the currency rates with base rate USD to currency rates with base rate EUR.

Upvotes: 1

Views: 207

Answers (3)

Steven Rumbalski
Steven Rumbalski

Reputation: 45552

Given:

rates = {
    'AED': 3.672782,
    'AFN': 57.680075,
    'ALL': 130.5485,
    'AMD': 477.866,
    'ANG': 1.78952,
    'EUR': 0.927815,
    'AOA': 107.1352,
    'USD': 1.0 } # base in rates needed to convert to/from base

rates can be used to support arbitrary conversions:

def convert_currency(rates, value, src_curr, dest_curr):
    return value / rates[src_curr] * rates[dest_curr]

In use:

>>> convert_currency(3.50, 'ANG', 'EUR')
1.81465001788189
>>> convert_currency(1.00, 'USD', 'EUR')
0.927815

To change rates to a new base do:

def rates_with_new_base(rates, old_base, new_base):
    new_rates = {curr: rate/rates[new_base] for curr, rate in rates.items()}
    # only needed if old base was not in rates table
    new_rates[old_base] = 1.0 / rates[new_base]
    return new_rates

In use:

>>> rates_with_new_base(rates, 'USD', 'EUR')
{'AED': 3.9585283704186724,
 'AFN': 62.16764656747305,
 'ALL': 140.70531302037583,
 'AMD': 515.0444862391748,
 'ANG': 1.9287465712453453,
 'AOA': 115.47043322214019,
 'EUR': 1.0,
 'USD': 1.0778010702564629}

Upvotes: 1

rth
rth

Reputation: 11201

Given a dictionary of rates,

 rates_usd = {"base": "USD", "rates": { "AED": 3.672782, "AFN": 57.680075, "ALL": 130.5485, "AMD": 477.866, "ANG": 1.78952, "EUR": 0.927815, "AOA": 107.13525}}

you can transform these values to a new base currency with,

def conversion(rates, base="EUR"):
     out = {'base': base, 'rates': {}}
     scale = rates['rates'][base]
     out['rates']['USD'] = 1./scale
     for key, val in rates['rates'].items():
         out['rates'][key] = val/scale
     return out

conversion(rates_usd, 'EUR')
# {'base': 'EUR',  'rates': {'AED': 3.9585283704186724,   'AFN': 62.16764656747305,   'ALL': 140.70531302037583,   'AMD': 515.0444862391748,   'ANG': 1.9287465712453453,   'AOA': 115.47048711219371,   'EUR': 1.0,   'USD': 1.0778010}}

Upvotes: 1

A.J. Uppal
A.J. Uppal

Reputation: 19264

Forgive me if I misunderstand your intention, but I believe you are asking for a USD to EUR conversion function:

def convert(usd, cur):
    rates = {
    "AED": 3.672782,
    "AFN": 57.680075,
    "ALL": 130.5485,
    "AMD": 477.866,
    "ANG": 1.78952,
    "EUR": 0.927815,
    "AOA": 107.13525,
    }
    return usd*rates[cur.upper()]

As such:

   convert(5, 'aed')
=> 18.363909999999997
   convert(5, 'aoa')
=> 535.67624999999998
   convert(5, 'eur')
=> 4.6390750000000001

Upvotes: 1

Related Questions