Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to limit output to 2 decimal points

i do have a small code when i run it, it gives me a very long decimal points how can short it to 2 decimal points,i tried using .replace but it doesn’t seem to work for me

<html lang="en-US" class="lang-en" dir="ltr">
  <head>
    <script type="text/javascript" src="//code.jquery.com/jquery-compat-git.js"></script>

  </head>
            
                  <input name="get" class="currencyField" value="12.99957024" >
                  <label id="price"></label>
                

          
          
    <script type="text/javascript">
    $(".currencyField").load('input', function(){ //input[name='calc']
        //input[name='calc']
        let convFrom;
        if ($(this).prop("name") == "get") {
          convFrom = "get";
          convTo = "usd";
        } else {
          convFrom = "usd";
          convTo = "get";
        }
        $.getJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", function (data) {
          var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
          var exchangeRate = parseInt(data[0].current_price);
          let amount;
          if (convFrom == "get") amount = parseFloat(origAmount * exchangeRate);
          else amount = parseFloat(origAmount / exchangeRate);

          let displayAmount = amount.toString();
          displayAmount = displayAmount.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
          $("input[name='" + convTo + "']").val(displayAmount);
          price.innerHTML = "$" + displayAmount;
          
        });
      });  
    </script>

   
  </body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use toFixed(2), add it to the end of lines 16 and 17

$(".currencyField").load('input', function() { //input[name='calc']
  //input[name='calc']
  let convFrom;
  if ($(this).prop("name") == "get") {
    convFrom = "get";
    convTo = "usd";
  } else {
    convFrom = "usd";
    convTo = "get";
  }
  $.getJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", function(data) {
    var origAmount = parseFloat($("input[name='" + convFrom + "']").val());
    var exchangeRate = parseInt(data[0].current_price);
    console.log(origAmount);
    let amount;
    if (convFrom == "get") amount = parseFloat(origAmount * exchangeRate).toFixed(2);
    else amount = parseFloat(origAmount / exchangeRate).toFixed(2);

    let displayAmount = amount.toString();
    displayAmount = displayAmount.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
    $("input[name='" + convTo + "']").val(displayAmount);
    price.innerHTML = "$" + displayAmount;

  });
});
<script type="text/javascript" src="//code.jquery.com/jquery-compat-git.js"></script>
<input name="get" class="currencyField" value="12.99957024">
<label id="price"></label>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading