A USD rate table, then a cross rate with made-up rates
Try it: Currency Converter — Live RatesOn this page
The currency converter does not know today's dirham rate until it downloads a table. The request is https://api.exchangerate-api.com/v4/latest/USD. The amount you type stays in the browser. The request does not include that amount. This post does not pretend a fixture is the live rate.
Input
Output
Input
Output
The formula
convertWithRates looks up both currencies in a map of "how many of this currency per 1 USD." Then:
result = (amount / rate[from]) * rate[to]
If either code is missing, or the source rate is not positive, the function returns null.
A cross rate from MAD to EUR is rate[EUR] / rate[MAD], via getCrossRate. You never need a direct MAD to EUR quote in the table. Both legs are against the dollar.
A fixture, not a market
I did not call the API for the numbers below. The map was written by hand so the arithmetic is obvious:
USD = 1
MAD = 10
EUR = 0.92
Read MAD = 10 as "10 dirhams per 1 dollar" inside this example only. It is not a rate from a bank and not a rate from the API on any date.
| Call | Result | | --- | --- | | 100 USD to MAD | 1000 | | 250 MAD to EUR | 23 |
The first is (100 / 1) * 10 = 1000. The second is (250 / 10) * 0.92 = 23. Both matched convertWithRates.
The same fixture's cross rate from MAD to EUR is 0.92 / 10 = 0.092. One dirham in the fixture is 0.092 euro, and 250 × 0.092 = 23. That is the same result as the two-step form. getCrossRate("MAD", "EUR", rates) is that 0.092. getCrossRate("USD", "USD", rates) is 1, because both legs are the dollar. A missing code returns null rather than zero. A zero source rate also returns null, because the function refuses to divide by it.
If the live table says something else, the page will show that something else. The label on the converter names the date field the API returned and the time the browser stored the table. The cache key is xsular-exchange-rates-v1 and the lifetime is one hour (EXCHANGE_RATES_CACHE_TTL_MS is 3,600,000 milliseconds). After that the page asks again. A cached table can be an hour old. A bank desk can be further from the mid figure than that.
Mistakes
Quoting the fixture as "the" dirham rate. It was chosen because the zeros are easy to check.
Assuming the page sends 250 to the rate host. It does not. It sends a request for the USD table. Your typed amount is an input to the multiply step after the table arrives.
Using the figure as the amount a bank will hand you. The table is a public mid-style feed. A transfer service adds a spread. This page does not add one, and it does not know the spread.
Forgetting that some currencies round to whole units. The file lists zero-decimal codes such as JPY, VND, and XOF. MAD is not in that set. A displayed dirham amount can include a fractional part even when a cash desk will not.
Mixing this up with spelling the amount. Writing "mille dirhams" is the number to words tool, and the Arabic output is not full cheque grammar. See that tool's own page before you put the words on a document. A percent off a price is the percentage calculator, which never sees the rate table.
Limits
If the request fails and no cached table exists, the screen is an error with a retry button. There is no baked-in dirham rate waiting behind it. While the first request is still running, the amount field is not shown. A later refresh can keep the previous table on screen and note that a newer fetch is in progress. I did not capture a live failure for this post. The behavior is what the component does when status is error or loading.
The list of currencies is the keys of that response, not a number I counted. Some codes are flagged as zero-decimal in the client (JPY, VND, and XOF are in that set). MAD is not. A dirham result can show a fraction even when a cash desk rounds to a whole dirham. The fixture results above are whole numbers only because I chose rates that divide evenly.
The hour cache means two people, or the same person before and after a refresh, can see different figures. Say which table date you used if you copy a number out.
The tool does not convert kilograms or kilometers. That is the unit converter, and it does not need a network call.

