I need to round the number that ends up

$("#total_video span").Math.round(summ); 

code itself

 $(function(){ $('#subscribe_video input:checkbox').on("change", function(){ var summ = 35.99; $('#subscribe_video input:checkbox:checked').each(function(index, element){ var add = parseInt($(element).val(), 10); if(!isNaN(add)) summ += add; }); $("#total_video span").text(summ); }) }); 
  • var summ = 36; ... $("#total_video span").text(summ); - Igor

1 answer 1

 var add = parseInt($(element).val(), 10); 
 var add = +$(element).val(); 
 $("#total_video span").text(summ); 
 $("#total_video span").text(Math.round(summ)); 
 $("#total_video span").text(summ.toFixed(2)); 

And the full code, but in a different way:

 $(function () { $('#subscribe_video').on("change", 'input[type="checkbox"]', function () { $("#total_video span").text(Array.prototype.reduce.call( $('#subscribe_video input[type="checkbox"]:checked'), function (sum, inp) { return +$(inp).val() + sum; }, 35.99 ).toFixed(2)); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="subscribe_video"> <label><input type=checkbox value="2.65">2.65</label> <label><input type=checkbox value="2.99">2.99</label> <label><input type=checkbox value="0.004">0.004</label> <label><input type=checkbox value="0.006">0.006</label> <label><input type=checkbox value="0.004">0.004</label> <label><input type=checkbox value="0.004">0.004</label> </div> <div id="total_video">Total: <span></span></div> 

  • and how to make it rounded to 29.99, and not to 30 - kirilly228
  • one
    @ kirilly228, do toFixed(2) - Grundy
  • can show where to put it? I just don't understand anything at all in js \ - kirilly228
  • @ Kirilly228, updated the answer, you can accept. - Qwertiy pm
  • @Qwertiy, well, at least add a few words about what you are doing in response. And what is your parseInt, by the way, correctly written, do not like :-) - Grundy