Back
Close

Currency Conversion Challenge

Statement

 Goal

In the IT business, prices are filled in local currencies meaning the currency of the country where the product is made.

But often prices are not expressed in the correct format, some providers can omit the "." or "," character if the amount is an integer (10 EUR / 10,00 EUR).
For the integer part of the amount, separator could be " " "," "." or "".

Price can be expressed in different formats:
|========================================|
|Country |Format |
|===============|========================|
|United States |1,234,567.89 USD |
|Canada |1,234,567.89 CAD |
|Great Britain |1.234.567,89 GBP |
|France |1.234.567,89 EUR |
|Germany |1 234 567,89 EUR |
|===============|========================|

The official number of decimal places to use are defined by ISO_4217 and could be 0, 2 or 3.

Currency conversion rates are usually given against USD.
1.00 USD = 0.907396 EUR
1.00 USD = 0.779168 GBP
.....

Given 1. The table of currencies:
|============================================================================|
|Currency|Conversion Rate against USD|Number of decimal to use from ISO_4217 |
|========|===========================|=======================================|
|USD |1 |2 |
|CAD |1.330037 |2 |
|GBP |0.779168 |2 |
|JPY |108.636533 |0 |
|BHD |0.376000 |3 |
|EUR |0.907385 |2 |
|============================================================================|

2. A string entry price expressed in various formats. Examples:
• 1.234.567,89 EUR
• 1 234.288 EUR
• 1.234,2 GBP
• 1 234 JPY

Note:
• See Constraints for valid formats.
price does not always follow the ISO_4217 rule for the number of decimals.
• Source currency will always be at the end after a space char (XXX XXX.XX EUR) .

3. A targetCurrency.

Provide the converted amount as a string in computer format (1234567.89) expressed in targetCurrency following ISO_4217 number of decimals.

Note: Amount result needs be floored, real conversion rates need to be used. If needed, trailing 0 need to be added to respect the decimal places defined.

More examples

Input:
1000 EUR
USD
Output:
1000 / 0.907385 = 1 102,068030...
Use 2 decimals and floor
1102.06

Input:
1000 EUR
EUR
Output:
1000.00
Input
Line 1: An integer N for the number for currencies defined.
Next N lines: A string currencyDefinition composed of Currency Code, Exchange rate against USD, Number of decimals to use in output, separated by spaces.
Line 2: A string price for the source amount.
Line 3: A string targetCurrency for the target currency to express the output.
Output
Line 1: A string with the converted amount in computer format, with correct number of digits.
Constraints
When price are expressed in EUR, decimal place will always be represented by "," characters but separator can be " " or "."
When price are not expressed in EUR, decimal place will always be represented by "." characters but separator can be " " or ","
Example
Input
6
USD 1 2
CAD 1.330037 2
GBP 0.779168 2
JPY 108.636533 0
BHD 0.376000 3
EUR 0.907385 2
1000 EUR
USD
Output
1102.06

Tags

Difficulty
Easy

Test cases
EUR to USD Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1000 EUR USD
Output
1102.06

Validator 1 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1010 EUR USD
Output
1113.08

EUR spaces to USD Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1 000 EUR USD
Output
1102.06

Validator 2 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1 010 EUR USD
Output
1113.08

EUR spaces and comma to USD Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1 000,0 EUR USD
Output
1102.06

Validator 3 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 1 010,0 EUR USD
Output
1113.08

CAD to EUR Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145,200.8 CAD EUR
Output
99059.67

Validator 4 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145,500.8 CAD EUR
Output
99264.33

GBP to JPY Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145 200.9 GBP JPY
Output
20244828

Validator 5 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145 500.9 GBP JPY
Output
20286656

JPY to BHD Test
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145,500,987,450 JPY BHD
Output
503590917.073

Validator 6 Validator
Input
6 USD 1 2 CAD 1.330037 2 GBP 0.779168 2 JPY 108.636533 0 BHD 0.376000 3 EUR 0.907385 2 145,900,987,450 JPY BHD
Output
504975350.062

Solution language

Solution

Stub generator input