I am currently working on a currency converter for use on a website, which will take a base price (euros) and convert this by a set variable, (GBP and USD values) with the result being displayed in the GBP and USD fields on the website.
My problem is that the results are currently showing with more than 2 decimals, example being 53.099999999999994 however I cannot seem to limit the resulting number to 2 decimal places, or round to the closest 10, 53.10 for example.
These numbers will change regularly, so ideally this will not be set as a variable, due to changes in exchange rates.
My current code is:
var euroConvert = 1;
var gbpConvert = 1.18;
var usdConvert = 1.21;
window.onload = function() {
document.getElementById('gbp1').innerText = "" + parseInt(document.getElementById('euro1').innerText) * gbpConvert;
document.getElementById('usd1').innerText = "" + parseInt(document.getElementById('euro1').innerText) * usdConvert;
document.getElementById('gbp2').innerText = "" + parseInt(document.getElementById('euro2').innerText) * gbpConvert;
document.getElementById('usd2').innerText = "" + parseInt(document.getElementById('euro2').innerText) * usdConvert;
}
With the "euro1" and "euro2" value being multiplied by the GBP and USD values, resulting in "gbp1", "gbp2", "usd1" and "usd2" values.
I am looking to limit the results for "gbp1", "gbp2", "usd1" and "usd2" to 2 decimal places...
(This is just an example using 2 values, the final website will have over 200 values in euros to be converted to GBP and USD)
I have tried using toFixed(2) as below:
document.getElementById('gbp1').innerText = "" + parseInt(document.getElementById('euro1').innerText) * gbpConvert.toFixed(2);
I have also tried:
var gbp1 = document.getElementById('gbp1').innerText;
parseFloat(gbp1.replace(/([^0-9\.])/g, ''));
alert(parsedPrice.toFixed(2));
I am expecting to have a resulting number which is limited to 2 decimal places, eg. 51.10