简体   繁体   中英

How do I display result to 2 decimal places?

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). Eg 23 M = 18.40 USD

...tried adding .toFixed(2); to various places but no luck.

You put it where you're outputting the value:

toShowResult.textContent = result.toFixed(2);
// ------------------------------^^^^^^^^^^^

Side note: Your code is relying on implicit conversion from string to number, and not doing that conversion in every place, so sometimes at the end result contains a string and other times it contains a number. Remember that the value on input elements is always a string.

Convert to number explicitly, and early:

var amount = +amountInput.value;
// ----------^
var to = +secondCurrencyInput.value;
// ------^

See this answer for your various string-to-number options, using a unary + as above is just one option.

Also remember that the standard IEEE-754 number type JavaScript uses is imprecise and not really well-suited to financial applications. You might consider JavaScript's new BigInt (converting to number and dividing by 100 for output), or until that's more broadly-supported, one of the many "big integer" or "big number" libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM