- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 09:31 AM
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;
}
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:25 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:08 AM
function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
var tot= 0.00;
var f1 = g_form.getDisplayValue('capital_outlay'); //Changed from getDecimalValue()
var f2 = g_form.getDisplayValue('labor_costs'); //Changed from getDecimalValue()
tot = f1+f2;
g_form.setValue('total_costs', "$"+tot); //Need commas and decimals to show properly;
}
//You can also use getCurrencyDisplayValue();
This should retain all currency formatting.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:03 AM
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 = parseFloat(f1+f2);
tot = "$"+tot;
g_form.setValue('total_costs', tot); //Need commas and decimals to show properly;
}
Please mark my response as correct/helpful based on the impact.
-Best Regards
Prateek kumar
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:05 AM
Thank you for your Response. Unfortunately this doesn't work either. Same results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 11:08 AM
This worked for me.
function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
var tot= 0;
alert(tot);
var f1 = g_form.getDecimalValue('u_captial_outlay'); //first currency field.
var f2 = g_form.getDecimalValue('u_labor_costs'); //second currency field
//tot = parseFloat(f1+f2);
//tot = parseFloat(f1)+parseFloat(f2);
tot = f1+f2;
alert(tot);
g_form.setValue('u_total_costs', currencyFormat(tot)); //Need commas and decimals to show properly;
}
function currencyFormat(num) {
return '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}
Please mark my response as correct/helpful based on the impact.
-Best Regards
Prateek kumar
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:25 AM
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.