Q:

Append ,00 if is a whole number

I need to append a decimal but with a coma so if is 300 it should change to 300,00 I have the code below and it works, but if the number already is a decimal it will change it to NAN. I need the code not to modify the numbers that already are decimals

    var formatter = new Intl.NumberFormat('es-ES', {
      style: 'currency',
      currency: 'USD',
    });
    $('.total-amount').each(function() {
      var x = $(this).text().replace(/[^\d,]/g, "");
      $(this).text(formatter.format(x));
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="total-in-basket">
  <div class="total-description">
    total is:
  </div>
   <p> with decimal example: </p>
  <div class="total-amount">
    $ 300,00
  </div>
  <p> without decimal example: </p>
  <div class="total-amount">
    $ 300
  </div>
</div>

A:

You can simply add one more extra replace to replace the trailing text after ( , )
Example:

        var formatter = new Intl.NumberFormat('es-ES', {
          style: 'currency',
          currency: 'USD',
        });
        $('.total-amount').each(function() {
          var x = $(this).text().replace(/[^\d,]/g,"").replace(/,.*$/g,""); /*<- add this*/ 
          $(this).text(formatter.format(x));
        });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="total-in-basket">
      <div class="total-description">
        total is:
      </div>
      <p> with decimal example: </p>
      <div class="total-amount">
        $ 300,00
      </div>
      <p> without decimal example: </p>
      <div class="total-amount">
        $ 300
      </div>
    </div>

