I'm trying to round the final result of my conversion to 2 decimal places, but I'm unsure where I need to add the .toFixed(2)
tried adding .toFixed(2); to various places but no luck. Everywhere that explains how to use it sets their own variable with a number value, but my result varies depending on the amount inputted/currency rate.
<div class="container">
<form class="form">
<input type="number" onkeypress="return noenter()" class="" id="amount"/>
</div>
<div style="display:none;">
<p id="currency-1">M</p>
</div>
<div class="">
<select class="form-control" id="currency-2" required>
<option>USD</option>
<option>GBP</option>
<option>EUR</option>
<option>CAD</option>
</select>
</div>
</form>
<div class="result">
<p>
<span class="given-amount"></span>
<span class="base-currency"></span>
<span class="final-result"></span>
<span class="second-currency"></span>
</p>
</div>
var crrncy = {
'M': {
'USD': 0.80,
'GBP': 0.65,
'EUR': 0.77,
'CAD': 0.95,
'M': 1
},
}
var baseCurrencyInput = document.getElementById('currency-1');
var secondCurrencyInput = document.getElementById('currency-2');
var amountInput = document.getElementById('amount');
var toShowAmount = document.querySelector('.given-amount');
var toShowBase = document.querySelector('.base-currency');
var toShowSecond = document.querySelector('.second-currency');
var toShowResult = document.querySelector('.final-result');
function convertCurrency(event) {
event.preventDefault();
var amount = amountInput.value;
var from = baseCurrencyInput.innerText;
var to = secondCurrencyInput.value;
var result = 0;
try {
if (from == to) {
result = amount;
} else {
result = amount * crrncy[from][to];
}
} catch (err) {
result = amount * (1 / crrncy[to][from]);
}
toShowAmount.innerHTML = amount;
toShowBase.textContent = from + ' would only cost you ';
toShowSecond.textContent = to;
toShowResult.textContent = result;
}
amountInput.addEventListener('keyup', convertCurrency);
secondCurrencyInput.addEventListener('click', convertCurrency);
function noenter() {
return !(window.event && window.event.keyCode == 13);
};
I expect the converter to display to 2 decimal places (and rounding up if possible). E.g. 23 M = 18.40 USD