Appear two decimal places

Asked

Viewed 1,168 times

-1

In this code I have, when value reaches 200.00... it shows 200, when it has decimal places, it shows decimal places... 200.55 = 200.55

However, I need him to show both decimals even if it’s "10.00", "00" after the point...

another thing is that I need to appear "," from time to time on the website print

I thought I’d put it, but I don’t know where I’d put it...

            total = parseFloat(total.toFixed(2));

jQuery(document).ready(function() {
    jQuery('select[name="service[]"]').change(function() {

        let selects = jQuery("select[name=\"service[]\"]");
        let total = 0;
        
        /* Percorre todos os select */
        $(selects).map(function(i, e) {
          let values = $(e).find(":selected");

       
          /* Percorre todos os valores selecionados */
          $(values).map(function(k, j) {
            total += parseFloat( $(j).data('price').replace(",", ".") );
          });
        });

        $("#preview_value").text(total);

    });
});  

  • 1

    You were able to resolve the https://answall.com/q/297362/8063 issue that you abandoned?

2 answers

1

Converting an integer to 2 decimal places.

<script>
var x = '146870';

    function format(num){
        return (num / 100).toFixed(2);
    }

    alert(format(x));
</script>

Exit will be 1468.70

1


Try this way:

 var total = 200;
 console.log(total.toFixed(2).replace(".", ","));

Edit. I added an example to better illustrate.

$('#numero').keyup(function(){
    $('#conversao').text(parseFloat($('#numero').val()).toFixed(2).replace(".", ","));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="numero"/>

<p id="conversao"></p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.