Cobra
Cobra

Reputation: 3

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?

Upvotes: 0

Views: 110

Answers (4)

Dgan
Dgan

Reputation: 10285

You can Use .toFixed(2) method in javascript

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

Upvotes: 1

baao
baao

Reputation: 73221

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)

Upvotes: 1

sol4me
sol4me

Reputation: 15698

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);

Upvotes: 1

kapantzak
kapantzak

Reputation: 11750

Use toFixed() javascript function like this:

newResult = result.toFixed(2);

result = the initial result with multiple decimals

newResult = the desired result (2 decimals)

Upvotes: 1

Related Questions