简体   繁体   中英

Formatting and adding decimals, commas in JQUERY

Hello got a question I'm trying to get this simple calculator to parse decimals correctly. Then also adding commas and decimals. I tried adding a parse int or float for the decimal and this didn't work or I didn't apply it correctly. I also tried the toFixed(2) for the decimals this also didn't work.

I posted the code here http://codepen.io/anon/pen/CGqAz .

All of the css,html and jquery is listed in this link but here is the jquery but it does not display the same as the code pen link.

$(document).ready(function () {

$("input").keyup(multInputs);
function multInputs() {
   var mult = 0;
   $(".calc").each(function () {           
       var $val1 = $('.start', this).val();
       var $val2 = $('.val2', this).val();
       var $val3 = $('.val3', this).val();
       var $val4 = $('.val4', this).val();
       var $val5 = $('.val5', this).val();
       var $val6 = $('.val6', this).val();
       var $val7 = $('.val7', this).val();
       var $val8 = $('.val8', this).val();
       var $val9 = $('.val9', this).val();
       var $val10 = $('.val10', this).val();
       var $val11 = $('.val11', this).val();
       var $val12 = $('.val12', this).val();
       var $total = $val1 - $val2 - $val3 - $val4 - $val5 - $val6 - $val7 - $val8 - $val9 - $val10 - $val11 - $val12;
       $('.totalbudget', this).text($total);
   });
}
});

This regex will add the appropriate commas to a number.

var $total = $val1 - $val2 - $val3 - $val4 - $val5 - $val6 - $val7 - $val8 - $val9 - $val10 - $val11 - $val12;
$total = $total.toFixed(2);
$total  = $total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
$('.totalbudget', this).text($total);

Edited to be a bit more complete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM