I want to do some math like plus, minus etc. with decimal values.
So i wrote two functions;
function to_decimal(i){
var $dec = parseFloat(i);
return $dec.toFixed(2);
}
function calc_price(){
var $t = $('#sub_total .total').text();
var $total = to_decimal($t);
$('#price_list ul li').each(function(){
var $p = to_decimal($(this).find('.item_price').text());
$total = $total + $p;
});
$t = $('#sub_total .total').text($total);
}
But these functions not working correctly i think because the result is returning string like 0.0010.30
Where is the problem?
You need to add a
+infront of that statement and you’re fine:That will convert the string into a number. If the string cannot get converted, it’ll return the
NaNvalue.