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

Question

Question

currency field

asked on February 1, 2021

I ran into an issue with the output of calculated sub-total and total fields. I need to calculate the amount based on unit quantity and price per unit in a table and after that I need a total for all the units together. I have that working fine. My issue is that when I put in the price in thousands per unit the subtotal and total fields gives an output in only 2 or 3 digits without showing the zeros. As you can see in the example below it shows the total to be 30 instead of 30,000 and 5 instead of 5,000 and 35 instead of 35,000. How can I format that?

 

Here is Java Script that I have right now:

 

$(document).ready(function () {
    $('.cf-table-block').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;
    }
});

 

 

Thank you!

Screenshot 2021-02-01 121021.png
0 0

Replies

replied on February 1, 2021

Hi Tetiana

Just an FYI, you can perform calculations in tables without using JS .

In the Calculate Total Price you can use a formula such as

example to 

=MULT(INDEX(tablename.Qtyfieldname,ROW(),INDEX(tablename.UnitPricefieldname,ROW())

2 0
replied on February 1, 2021

I think it is because you have the option to show commas, which converts the number fields to a text fields and the comma comes over with the value.

So you are trying to parseNumber("5,000") instead of parseNumber(5000)

You can either turn off the commas or use a function to convert the strings back to numbers.

I use the following function in my master library since I need to call it so often:

//Get a number from a forms number or currency field. Forms tends to send back a string, with commas.
function Num(strNumber) {
    strNumber += '';
    strNumber = strNumber.split(',').join('');
    return Number(strNumber);
}

 

1 0
replied on February 1, 2021

Turning off the commas fixed the problem. The function did not seem to work. If I use the function would I get the commas in the output? Is there a way to have commas in all the currency fields? 

Thank you so much for help!

0 0
replied on February 1, 2021

In the currency field, check the box for Delimiters

0 0
replied on February 1, 2021

I do have that box checked but it does not make a difference.

0 0
replied on February 1, 2021

The custom Num function will always return a number, no matter if you pass it a number or a string with commas. It is used like this

let value = $(field).val();
value = Num(value);

 

1 0
replied on February 1, 2021

Once you put the value in the field, you should trigger change such as 

    $('.total input').val(sum).change;  or     $('.total input').val(sum).on('change');

so that forms recognized the change to the field, and that the internal functions of the forms such as Rules, Formulas, etc.

Don't think it will help you as your JS maybe overriding the field formatting but something to look into. 

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

Sign in to reply to this post.