jquery - Up to 2 trailing digits after decimal -


following jquery code

    $('#cancelation').change(function(){                 if($(this).is(':checked')){             total = parsefloat($('#totalamount').val()) + parsefloat($('#cancelation').val());                               } else {         total = parsefloat($('#totalamount').val()) - parsefloat($('#cancelation').val());         }         $('#totalamount').val(total);         $('#totalprice').html(total);     }); 

if #totalamount value 10.49 shows correctly on page load , problem if checkbox checked or unchecked, value of checkbox 2.00 added or subtract #totalamount , #totalprice output shows 12.48999998 on add 0r 10.48999998 on subtract.

i tried .tofixed(2); result same, other way fix issue?

edited: html code

<label for="cancelation"><input type="checkbox" name="cancelation" id="cancelation" value="2.00"> cancellation protection: 2.00</label> <input type="text" name="totalamount" id="totalamount" value="10.49" /> total amount: <span id="totalprice">10.49</span> 

you can use math.round(number * 100) / 100 round 2 decimal places.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -