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

Question

Question

Purchase Order Sample code Rounding Problem

asked on November 11, 2014

I am new to Forms, and a Purchase Order request form is my first one.  The example code I found on the LF support site works great:

$(document).ready(function () {
    $('.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);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
        });
        $('.total input').val(sum);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});
 

The problem I am having here is that some combinations of quantity/price will result in the extended price (subtotal) going out way past 2 decimal places (example.....14 items @ $14.63 results in a subtotal of $204.82000000000002).

 

This throws the total after tax and shipping into an "Invalid input."

 

How can I force the subtotal and total amounts to always be rounded to 2 decimal places?

 

Thank You

0 0

Replies

replied on November 13, 2014
$(document).ready(function () {
    $('.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;
            sum += ( parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val()) );
        });
        $('.total input').val(sum.toFixed(2));
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

Give that a try. .toFixed converts a number to a string. Using it when storing to a field is the ideal method as you get the value in a field that you do not need to have the value stored as a number for anymore. 

1 0
replied on November 11, 2014

you should add code like the following to a value

.toFixed(2);

 

0 0
replied on November 11, 2014

I tried that with no luck......

 

Maybe I was ading it wrong.  Where in that code and would it be:

 

s.toFixed(2);

0 0
replied on November 11, 2014

can you please add your code in a code section so it's easier to respond with modified code. I believe you would want to attempt this in the parseNumber function

var f = parseFloat(n).toFixed(2);

 

0 0
replied on November 13, 2014

Still getting the same result.  Code:

$(document).ready(function () {
    $('.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);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
        });
        $('.total input').val(sum);
    }
    function parseNumber(n) {
        var f = parseFloat(n).toFixed(2); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

0 0
replied on November 13, 2014

Added the code in a code snippet as Javascript but still doesn't look like yours.....sorry bout that

0 0
replied on November 13, 2014

WORKED......awesome.....thank you

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

Sign in to reply to this post.