Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

format Currency with Comma's and decimal points

Peter Williams
Kilo Sage

Hello everyone,

i created a widget to calculate the totals for a form i created in the catalog item.

But i need to be able to have the totals formatted for currency, meaning the commas to appear for the thousands and so on.

 

here is my current script:

 

api.controller = function($scope) {
var g_form = $scope.page.g_form;

// Watch for changes in all two MRVs
$scope.$watchGroup([
function() {
return g_form.getValue('bill_to_office_mrv');
},
function() {
return g_form.getValue('disbursements_client');
},
], function(newValues) {
var grandTotal = 0;
var office = 0;
var client = 0;
for (var i = 0; i < newValues.length; i++) {
var mrvValue = newValues[i];
if (mrvValue) {
var jsonData = JSON.parse(mrvValue);
var amountFieldName = "";
if (i === 0) {
amountFieldName = "amount_office"; // amount field name for bill_office_mrv
} else if (i === 1) {
amountFieldName = "amount_client"; // amount field name for bill_client_mrv
}

// Calculate the total for the current MRV
var total = 0;
for (var j = 0; j < jsonData.length; j++) {
var amountValue = parseFloat(jsonData[j][amountFieldName]) || 0;
total += amountValue;
}

// Add the current MRV's total to the grand total and respective variable
grandTotal += total;
if (i === 0) {
office = total;
} else if (i === 1) {
client = total;
}

// Set the total for the current MRV
var mrvTotalFieldName = "";
if (i === 0) {
mrvTotalFieldName = "total_office"; // total field name for bill_office_mrv
} else if (i === 1) {
mrvTotalFieldName = "total_client"; // total field name for bill_client_mrv
}
g_form.setValue(mrvTotalFieldName, total.toFixed(2) +" $" );
}
}

// Set the grand total and respective variable
g_form.setValue("grand_total", grandTotal.toFixed(2)+" $");
$scope.expenses = {
office: office.toFixed(2) +" $",
client: client.toFixed(2) +" $",
grandTotal: grandTotal.toFixed(2)+" $"
};
});
};

 

 

any help is appricated 

 

2 REPLIES 2

Maik Skoddow
Tera Patron
Tera Patron

Hi

formatting of numbers is a system-wide configuration (see https://docs.servicenow.com/en-US/bundle/utah-platform-administration/page/administer/currency/conce...) and applies to all numbers in all tables.

If your system locale is set to a language/country with the formatting "000,000.00" it is not recommended to bypass this with any custom implementations/displays. In the worst case you store the wrong numbers in your tables.

Maik

ah so there isn't anyway i could format the string to include the commas in it