I can not figure out why only integers are added / minus

There is a small calculator with hidden input, in which there is a value value, when you press "+", "-", addition / subtraction should be in that number and decimal number. Now only integers are added.

https://jsfiddle.net/L08danx8/

<form action="#" method="get" name="form-kitchen"> <table cellpadding='0' cellspacing='0' class='modal-kitchen__calc'> <td> <input type='hidden' name='price' value='139.90'> </td> <td> <div class='calc__value'> <span class="value__text">Количество: </span> <input type='text' onkeyup="this.value=this.value.replace(/[^\d\.]+/g,'')" name='col' value='0' /> <a class='minus'>-</a> <a class='plus'>+</a> </div> </td> <td class="modal-kitchen__sum">руб.</td> </table> <input type="submit" class="form-kitchen__btn" value="Заказать"> </form> 

Js

  var order={}; $('.minus').click(function () { var $input = $(this).parent().find('input'); var count = parseInt($input.val()) - 1.0; count = count < 0 ? 0 : count; $input.val(count); $input.change(); return false; }); $('.plus').click(function () { var $input = $(this).parent().find('input'); $input.val(parseInt($input.val()) + 1.0); $input.change(); return false; }); $('.modal-kitchen__calc').on('change','.calc__value input',calculate_price); function calculate_price() { var $input=$(this), count=$input.val(), price=parseFloat($input.parents('td').prev().children('input').val()), sum=parseInt(price*count), item=$input.parents('tr').children('tr').eq(2); $input.parents('td').next().text((print_price(sum))).data('sum',sum); if (count===0) { delete(order[num]); calc_sum(); return; } order[item]={sum: sum,count: count}; calc_sum(); } function calc_sum() { var sum=0; for (var key in order) { sum+=order[key].sum; } $('.modal-kitchen__calc').find('td').last().text(print_price(sum)); } function print_price(v) { return ((v*100)+'').replace(/(..)$/,',$1 руб.') } 

    1 answer 1

    Remove parseInt from line

    sum=parseInt(price*count),

    both factors — price and count — are already numbers, and their product, naturally, is also a number.

    Update

     function print_price(v) { var a = v.toFixed(2); return a + " руб."; } 

    Update 2

     $(document).ready(function(){ $(".calc__value").find('input').change(); // или $(".calc__value input").change(); }); 
    • Thanks, it helped. But now every third iteration does not round. Note 41970.000000000.01 - atomr
    • @atomr What are the iterations? What is it about? - Igor
    • In your last update, the question was resolved, thank you very much again - atomr
    • @atomr Not at all. Successes in programming! - Igor
    • Hello again. Faced such a problem, I do not understand how to solve. If in the input of the quantity value is already "1" by default, then why is the price of a single product not transmitted when the page is loaded? Look, please, if it is not difficult jsfiddle.net/atomr/L08danx8/2 - atomr