0

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

Ryan1477
  • 3
  • 2
  • You are not doing toFixed on the result. You are going it just to the one number you are multiplying on. You need to do toFixed on the result, not the initial numbers.... `var total = a * b; const var result = total.toFixed(2);` – epascarello Jan 30 '23 at 22:16
  • `"" +` no reason to do that – epascarello Jan 30 '23 at 22:18
  • Apply the `toFixed` to the final result. So not `parseInt(...) * gbpConvert.toFixed(2);` but `(parseInt(...) * gbpConvert).toFixed(2);`. – Mike 'Pomax' Kamermans Jan 30 '23 at 22:18
  • @Mike'Pomax'Kamermans Tank you, this has worked exactly as wanted. Think I had previously tried to set the .toFixed(2) on the result, but had not added () around the parseInt and result. – Ryan1477 Jan 30 '23 at 22:24
  • General, you can't round number to 2 decimal places, because number are stored as binary floats. Example: If `1.21` is scanned, it is stored to the nearest possible number `1.20999999999999996`. Proof it with `(1.21).toPrecision(18) `. Exceptions are numbers like `1.5`or `1.25` (multiple of `0.25` for 2 fraction digits). – Wiimm Feb 03 '23 at 12:59
  • If you really need exact numbers with 2 digits, than store them as integers with factor 100. And if displaying them, use something like `(number/100).toFixed(2)`. – Wiimm Feb 03 '23 at 13:00

0 Answers0