You are viewing limited content. For full access, please sign in.

Question

Question

Weird calculation after implementing thousand delimiter for Forms 9.2

asked on March 28, 2017

After implementing the thousand delimiter for Forms 9.2, my calculation went haywire.  Could someone take a look at the code and let me know where the issue lies?  Thanks.

 

 

$(document).ready(function () {
  $('.table').on('blur', 'input', sumtotal)
   //$('#q17').on('blur', 'input', sumtotal)
   $('#q19').on('blur', 'input', sumtotal)
    //product table function
    function sumtotal() {
      	$('.total input').attr('readonly', false);
        var quantity = 0;
      	var unit = 0;
      	var total = 0;
      	var subtotal = 0;
      	var shipping = $('#q19 input').val();
        var gradtotal = 0;
      	      
        $('.table.cf-table-block tbody tr').each(function () {
            quantity =  parseNumber($(this).find('.quantity input').val()); 
            unit = parseNumber($(this).find('.unit input').val());                    
            subtotal = parseNumber(quantity * unit);

            $(this).find('.total input').val(subtotal.toFixed(2));
            total += subtotal;
            $(this).find('.total input').change();
          
        });
          
		grandtotal = parseNumber(shipping)+parseNumber(total)//+(total*$('#q17 input').val());
		$('#q18 input').val(grandtotal);		
        $('#q18 input').change();

            
		//console.log("Total " + total);
		//console.log("Shipping " + shipping);  
		//console.log("Grand totol " + grandtotal);
    }
  

  
  	//parse for number value function
    function parseNumber(n) {
      if (typeof n === 'string' || n instanceof String)
      	n = n.replace(/,/g, '');	

        var f = parseFloat(n); //Convert to float number.     
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
  
	//Format numbers with commas
	$(".unit input, .etotal input, .shipping input").attr("pattern", '^(\\d+|\\d{1,3}(,\\d{3})*)(\\.\\d{2})?$');
	$(".unit input, .etotal input, .shipping input, #q18 input, .total input").on("change", function () {
		$(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
	});

});

 

0 0

Answer

SELECTED ANSWER
replied on March 28, 2017

Hi Tommy,

Calculating 39*3.22 by javascript would result in 125.58000000000001, so grandtotal would have value 127.80000000000001. When update it with replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") the result would be "127.80,000,000,000,001"

For your code, you could change $('#q18 input').val(grandtotal);   to $('#q18 input').val(grandtotal.toFixed(2)); to avoid the issue.

1 0
replied on March 29, 2017

Thank you.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.