Search code examples
javascriptjquerydecimallimit

Limit results on 2 decimal


I have this script:

 jQuery(document).ready(function($){
    setTimeout(function(){ 
        var total_share = $('.irpg_totalcount .irpg_t_nb').text();
        var total_share = parseFloat(total_share);
        if(total_share==0) { total_share = 1; }
        var value_share = 1000 / total_share;
        $('.share_info span').text(value_share);
    }, 3000);

});

Some results displaying for example:

3333.333333333

How to limited results on two decimal?


Solution

  • You can Use .toFixed(2) method in javascript

    $('.share_info span').text(parseFloat(value_share).toFixed(2))