How to truncate with jQuery

117 Views Asked by At

I have a list <li>Baths 2.00</li> and i want to remove .00 to show only the decimal 2

How i can remove the decimal part with jQuery.

Do you have any solutions?

Regards

2

There are 2 best solutions below

0
On

you should use toFixed :

123.45.toFixed(0) //123

And it's not related to jQuery but pure JS.

regarding your html : (assuming the numbers are at last place)

$("li").text(function (i,n){  var g=$(this).text().split(' ');
                            g[g.length-1]= parseFloat(g[g.length-1],10).toFixed(0); return g.join(' ');})

http://jsbin.com/ifoZOjE/3/edit

0
On

Here is some example mate..Hope this might help u ...:) IF you have the part that contains the number in a variable or something use the below

 var iNum = 5.123456;

The toPrecision() method formats a number to a specified length.

  iNum.toPrecision();    // Returns 5.123456
  iNum.toPrecision(5);   // Returns 5.1235
  iNum.toPrecision(1);   // Returns 5

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

  iNum.toFixed(2);        // Returns "5.12"

//Removes the part after the point.Be sure there is not point part that comes in the word part..:)

var value = $('li').text();
$('li').text(value.split('.')[0])