Calculating currency field - Result doesnt have proper format

Chad31
Mega Expert

When using a script to calculate two currency fields and show the sum in a 3rd currency field it's not returning the proper formatting. 

function onLoad(control, oldValue, newValue, isLoading, isTemplate) {

var tot= 0.00;
var f1 = g_form.getDecimalValue('capital_outlay');   //first currency field.  
var f2 = g_form.getDecimalValue('labor_costs'); //second currency field

tot = f1+f2;
g_form.setValue('total_costs', "$"+tot); //Need commas and decimals to show properly;
}

find_real_file.png

Decimals are showing correctly but other numbers don't have commas. I have seen several post on this but don't know how to adapt answers to my solution. Any advise would be helpful. Thank you

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Chad,

 

Please try the below script. I have tried and executed. The code works as expected:

 

var tot= 0.00;

var f1 = g_form.getDecimalValue('capital_outlay'); // f1=505.89
var f2 = g_form.getDecimalValue('labor_costs');    // f2=5000.00


tot = f1+f2;
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}

var sum=formatNumber(tot));


g_form.setValue('total_costs',sum);

 

Output:

*** Script: Total::::5,505.89

 

Please mark the answer helpful if it helps.

View solution in original post

10 REPLIES 10

That worked Perfectly. Thank you 

 

 

function onLoad(control, oldValue, newValue, isLoading, isTemplate) {

var tot= 0.00;
g_form.setValue('total_costs', '');
var f1 = g_form.getDecimalValue('capital_outlay'); //first currency field. 
var f2 = g_form.getDecimalValue('labor_costs'); //second currency field

tot = parseFloat(f1+f2);
tot = "$"+tot;
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
g_form.setValue('total_costs',formatNumber(tot)); //Need commas and decimals to show properly;
}

This is the final version slightly updated in case anybody needs it in the future. Thank you!