The CreatorCon Call for Content is officially open! Get started here.

Adding 2 Variable fields together that includes 2 decimal places to populate a third value

jonathangilbert
Kilo Sage

Hi,

I have been on the forums to find how I add 2 variable fields to popluate a third. I have come across this script:- 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var value1 = parseInt(g_form.getValue("tax_amount_in_document_currency"));
    var value2 = parseInt(g_form.getValue("amount_in_document_currency_ACGL_ITEMWRBTR01"));

var sum = value1 + value2;
   
g_form.setValue('amount_in_document_currency', sum.toString());
}
 
 
 
It works for whole numbers (i.e if the value1 is 100.00, and value2 is 20, it would populate 120.
 
 
 
but if it is value1 is 100.10 and value2 is 20.02 it is populating 120 instead of 120.12, it is not including the values after the decimal places. Any ideas what else I need to do?
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Jonathan,

Use parseFloat instead of parseInt.  Check out this post for more options and examples.

https://www.servicenow.com/community/developer-articles/useful-number-methods-in-javascript/ta-p/232... 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Jonathan,

Use parseFloat instead of parseInt.  Check out this post for more options and examples.

https://www.servicenow.com/community/developer-articles/useful-number-methods-in-javascript/ta-p/232... 

Morning Brad,

That is great, thank you!, it works perfectly. I have also added .toFixed(2) aswell, so it always gives 2 decimal places. 

Good to hear.  You are welcome!