On my purchase order form, I had to change the "Price" column of my table to be a number rather than currency, as I needed my users to be able to put in pricing that reflected more than 2 decimal places. When the calculation is running through javascript to multiply the "Quantity" value by the "Price" value, the result is not being rounded. Does the Math.round function round on a 5/4 split? If so, can someone tell me how to implement this into my existing script? I tried the script to force all currency fields to round on change, but that is not working. For instance, I can put a quantity of 177 into my form with a price of 0.205, and the result is displaying $36.28; however, if it were rounding on a 5/4 split, it should display $36.29.
$(document).ready(function(){ //hide Hidden User field var $hidden = $('#q71'); $hidden.hide(); //hide Hidden Company Name field var $hidden = $('#q37'); $hidden.hide(); //hide Hidden Vendor Type field var $hidden = $('#q73'); $hidden.hide(); //hide Hidden Vendor Name field var $hidden = $('#q75'); $hidden.hide(); //Round any entries made to the Ext. Total field $('.currency').change(function() { $(this).val(formatCurrency($(this).val())); }); function formatCurrency(value) { var float = parseFloat(value); return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2); } //Calculate line item totals, subtotal, and grand total $('.cf-table-block').on('blur', 'input', sumtotal); $('.tax').on('blur', 'input', sumtotal); $('.shipping').on('blur', 'input', sumtotal); function sumtotal() { var sum = 0; $('.cf-table-block tbody tr').each(function () { var s = 0; s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val()); $(this).find('.subtotal input').val((s).toFixed(2)); sum += s; }); $('.total input').val((sum).toFixed(2)); $('.grandTotal input').val((sum + parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val())).toFixed(2)); $('.ro input').attr('readonly', 'True'); //makes the fields read only after we give them a value. } function parseNumber(n) { var f = parseFloat(n); //Convert to float number. return isNaN(f) ? 0 : f; //treat invalid input as 0; } //SET THE HIDDEN VENDOR NAME VARIABLE FOR USE WHEN SAVING TO LASERFICHE: //Whenever the Vendor or New Vendor fields are changed, if the New Vendor field is empty, //fill the Hidden Vendor Name field with the value from the Vendor field. //Otherwise, fill the Hidden Vendor Name field with the value from the New Vendor field. $('.vendor select').change(grabVend); $('.newvendor input').change(grabVend); function grabVend(){ var hdnvndnm = 0; var vndnm = 0; vndnm = $('.vendor select').val(); console.log(vndnm); var newvend = 0; newvend = $('.newvendor input').val(); console.log(newvend); if ((!$('.newvendor input').val())) { $('.hdnvendor input').val(vndnm); } else { $('.hdnvendor input').val(newvend); } hdnvndnm = $('.hdnvendor input').val(); console.log(hdnvndnm); } //handle other scripted activities here });
Thanks for any help you can provide.