0

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

});
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Bizboss
  • 7,792
  • 27
  • 109
  • 174

3 Answers3

1

.replace("," , ".")

http://jsfiddle.net/QmTNZ/32/

more to read:

How to convert string into float in javascript?

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
0

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() ) );
Bakudan
  • 19,134
  • 9
  • 53
  • 73
0

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/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317