0

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?

Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
Cobra
  • 3
  • 3
  • try this `3333.333333333.toFixed(2);` – Kirill Khrapkov Dec 30 '14 at 09:44
  • possible duplicate of [JavaScript: Round to a number of decimal places, but strip extra zeros](http://stackoverflow.com/questions/7312468/javascript-round-to-a-number-of-decimal-places-but-strip-extra-zeros) – JLRishe Dec 30 '14 at 10:01

4 Answers4

1

Use toFixed() javascript function like this:

newResult = result.toFixed(2);

result = the initial result with multiple decimals

newResult = the desired result (2 decimals)

kapantzak
  • 11,610
  • 4
  • 39
  • 61
1

You can Use .toFixed(2) method in javascript

$('.share_info span').text(parseFloat(value_share).toFixed(2))
Koen.
  • 25,449
  • 7
  • 83
  • 78
Dgan
  • 10,077
  • 1
  • 29
  • 51
1

You can use toFixed.

The toFixed() method converts a number into a string, keeping a specified number of decimals.

var num = 3333.333333333     
var n = num.toFixed(2);
sol4me
  • 15,233
  • 5
  • 34
  • 34
1

Examples for toFixed:

var numObj = 12345.6789;

numObj.toFixed();       // Returns '12346': note rounding, no fractional part
numObj.toFixed(1);      // Returns '12345.7': note rounding
numObj.toFixed(6);      // Returns '12345.678900': note added zeros
(1.23e+20).toFixed(2);  // Returns '123000000000000000000.00'
(1.23e-10).toFixed(2);  // Returns '0.00'
2.34.toFixed(1);        // Returns '2.3'
-2.34.toFixed(1);       // Returns -2.3 (due to operator precedence, negative number literals don't return a string...)
(-2.34).toFixed(1);     // Returns '-2.3' (...unless you use parentheses)
baao
  • 71,625
  • 17
  • 143
  • 203