Tip:
A better way to do this is:
1. Define a reference currency.
2. Get amount to convert from user.
3. Get original currency from user.
4. Get desired currency from user.
Calculate like this (pseudo-code):
1 2
|
intermediateAmount = ConvertToReferenceCurrency(inputAmount, originalCurrency)
desiredResult = ConvertFromReferenceCurrency(intermediateAmount, desiredCurrency)
|
Easy as pie. All you have to do is get the currency exchange rate of the currencies you are going to support based solely on one currency: Your reference currency.
Example:
Reference currency: U. S. Dollar
Yen exchange rate: yyy yens per U. S. Dollar
Euro exchange rate: zzz euros per U. S. Dollar
etc.
Notice how all values are "per U. S. Dollar", the reference currency.
Now, ConvertToReferenceCurrency() calculates as:
result = amount / originalCurrencyExchangeRate
Similarly, ConvertFromReferenceCurrency() calculates as:
result = amount * destinationCurrencyExchangeRate
If you do this like this, you are free to add support for new currencies by just adding one currency exchange rate value, and you can convert any currency to any other currency without additional variables, meaning you don't have to pair currencies up (dollarsToYens, YensToDollars, YensToEuros, EurosToYens, etc. :( neverending :-S ).