Reputation: 913
I've got an API that returns the value of: USD/Bitcoin & USD/GBP.
{"USDBITCOIN":"4251.27", "USDGBP":"0.758659"}
To calculate the exchange rate of GBP/Bitcoin, I simply divide the the value of the GBP by Bitcoin, because they are both USD based.
How would calculate the exchange rate of Bitcoin/GBP? It may be really simple, but it's really baffling me. I've got the below to calculate
app.js
xOfy(unit, value) {
return unit / value;
}
yOfX(unit, value) {
return unit * value;
}
xOfy(gbp, bitcoin)
// 0.00018
yOfx(bitcoin, gbp)
// 3225.26
Upvotes: 2
Views: 397
Reputation: 66173
In order to calculate Bitcoin per GBP, you simply use USD per GBP divided by USD per Bitcoin. Given that:
0.758659
and4251.27
…then Bitcoin per GBP can be represented using the following fraction: 0.758659 ÷ 4251.27
.
A little bit of arithmetic will help to explain this. Just run the code snippet so MathJax will draw the formula to describe the calculation above:
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS-MML_HTMLorMML&dummy=.js"></script>
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} }); </script>
<p>Given the following conversion rates:</p>
\[
\require{cancel}
\begin{aligned}
\frac{\mathrm{USD}}{\mathrm{Bitcoin}} & = 4251.27
\\~\\
\frac{\mathrm{USD}}{\mathrm{GBP}} & = 0.758659
\end{aligned}
\]
<p>So, if you want to compute GBP per bitcoin, you will need this mathematical transformation:</p>
\[
\require{cancel}
\begin{aligned}
\frac{\mathrm{Bitcoin}}{\mathrm{GBP}}
& = \frac{\mathrm{Bitcoin}}{\cancel{\mathrm{USD}}} \times \frac{\cancel{\mathrm{USD}}}{\mathrm{GBP}} \\
& = \frac{\mathrm{Bitcoin}}{\mathrm{USD}} \times \frac{\mathrm{USD}}{\mathrm{GBP}} \\
& = \frac{1}{\frac{\mathrm{Bitcoin}}{\mathrm{USD}}} \times \frac{\mathrm{USD}}{\mathrm{GBP}} \\
& = \frac{1}{4251.27} \times 0.758659 \\
& = \frac{0.758659}{4251.27} \\
& = 0.00017848
\end{aligned}
\]
Upvotes: 1
Reputation: 128776
You simply divide 1 by the resulting value:
1 / (USDGBP * USDBITCOIN)
In this case, that'd be:
1 / (0.758659 * 4251.27) = 0.0003100521146296338
The 1 itself comes from the fact 4251.27 USD is how much 1 Bitcoin is worth.
Upvotes: 1