Reputation: 7882
I want to improve this code http://jsfiddle.net/QmTNZ/30/ to use comma separated price in place of the point
as you we can seem when we use for example 1 x 375,5 I get 375 in the Sum and not 375,5
My code:
function ca(){
var $overall = 0;
$("tr.sum").each(function() {
var $qnt = $(this).find(".qty");
var $price = $(this).find("td").eq(1);
console.log($qnt + " | " + $price);
var sum = parseFloat($price.text()) * parseFloat($qnt.val());
if(isNaN(sum)) {
sum = 0;
}
$(this).find("td").eq(2).text(Math.round(sum * 100) / 100);
$overall += sum;
});
$("#total").text($overall);
}
$(function() {
ca();
$('input.qty').bind('change keyup',function(){ca();});
});
Upvotes: 0
Views: 2315
Reputation: 19492
In your html use are using both "," and "."
JavaScript uses "." for float numbers. "," is used for enumeration. Better change the prices to xx.yy rather than xx,yy .
For example this won`t work
var a = 12,1;
var b = 12.4, c = 0;
c = a + b;
You may also change this line ( objects tends to be integer in most of the cases :) )
var sum = parseFloat($price.text()) * parseFloat($qnt.val());
to
var sum = parseFloat($price.text()) * parseFloat( parseInt ($qnt.val() ) );
Upvotes: 0
Reputation: 196002
Change ,
to .
before parsing the price and use .toFixed(2)
to force 2 decimals at the results.
working demo at http://jsfiddle.net/gaby/QmTNZ/38/
Upvotes: 0
Reputation: 40651
.replace("," , ".")
more to read:
How to convert string into float in javascript?
Upvotes: 1