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

Question

Question

Format forms number field to accept decimal values, including script for sum of those fields

asked on February 26, 2016 Show version history

I have a form (absence request) that has a table containing a number field (number of hours requested).  The user can click a link to add more lines to the table, and I have a number field that contains the sum of all hours requested.  The script I have for the sum is posted below, but what I need is to incorporate script to allow decimal (2 places) values in both the table number field and the sum number field.  I see some script across the Answers site related to currency, etc., but I don't know how to incorporate the script to allow decimals with the script I already have.  Any help to get this functionality added to my form would be greatly appreciated.  

$(document).ready(function () {
    $('.cf-table-block').on('blur', 'input', sumtotal);
  
    function sumtotal() {
        var s = 0;
        $('.sum input').each(function () {
            s += parseNumber($(this).val());
        });
        $('.total input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }

});

 

0 0

Replies

replied on February 26, 2016

Hi Melanie,

I haven't taken a good look at the setup but it sounds like you want the .toFixed(n) method from JavaScript; you can refer to this thread for an example of usage. Note that in Forms 10, setting a number of decimal places can be configured for number fields from the Forms builder UI, and there is a new field type for handling currency in particular (if that is your use case).

Hope this helps!

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

Sign in to reply to this post.