Expand my Community achievements bar.

Adobe Campaign User Groups are live now. Join our Adobe Campaign User Groups and connect with your local leaders!
SOLVED

Format vars.recCount value for an alert?

Avatar

Level 5

I'm using JS to formt values. Insted of 1000, I would like it to be 1,000 on the final alert sent to the imbox of users.

I've found this formatter func online, but it is giving me errors:

const formatter = new Intl.NumberFormat('undefined', {
style: 'currency',
currency: 'USD',

// These options are needed to round to whole numbers if that's what you want.
//minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
//maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
});

vars.field1 = formatter.format(vars.recCount)); /* $2,500.00 */


Why?

09/03/2024 5:11:37 PM SCR-160032 Javascript: error while compiling script 'WKF41027/js'.
09/03/2024 5:11:37 PM JST-310000 Error while compiling script 'WKF41027/js' line 11: missing ; before statement (line='vars.field1 = formatter.format(vars.recCount)); /* $2,500.00 */ ' token='); /* $2,500.00 */ ').

The code uses currency, but I'd like just render this as numbers.

This is the alert sent as an email:

Hoy <%= formatDate(new Date(), "%2D/%2M/%4Y") %> , la cantidad de recipients con email comercial es:  <%= vars.field1 %>    y la cantidad de celular comerciales es <%= vars.field2 %> .


1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @ogonzalesdiaz  You can use this code:

function addThousandSeparator(number_as_string) {  
  let parts = number_as_string.split('.');
  let integerPart = parts[0];
  let decimalPart = parts.length > 1 ? '.' + parts[1] : '';   
  integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ','); 
  return integerPart + decimalPart;
}

 

You can use it like this:

addThousandSeparator(vars.recCount);

     Manoj
     Find me on LinkedIn

View solution in original post

5 Replies

Avatar

Correct answer by
Community Advisor

Hello @ogonzalesdiaz  You can use this code:

function addThousandSeparator(number_as_string) {  
  let parts = number_as_string.split('.');
  let integerPart = parts[0];
  let decimalPart = parts.length > 1 ? '.' + parts[1] : '';   
  integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ','); 
  return integerPart + decimalPart;
}

 

You can use it like this:

addThousandSeparator(vars.recCount);

     Manoj
     Find me on LinkedIn

Avatar

Level 5

Hi Manjo, got error: 09/04/2024 10:28:35 AM SCR-160032 Javascript&colon; error while compiling script 'WKF41xxx/js'.
09/04/2024 10:28:35 AM JST-310000 Error while compiling script 'WKF41xxx/js' line 3: missing ; before statement (line=' let parts = number_as_string.split('.'); ' token='parts = number_as_string.split('.'); ').

ogonzalesdiaz_0-1725464544155.png

 



Avatar

Community Advisor

Hello @ogonzalesdiaz  here is the simplified version

 

function addThousandSeparator(myCount) {
return myCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}


     Manoj
     Find me on LinkedIn

Avatar

Level 5

My ACC instance only works with legacy JS. 

So, this code did work:

function addThousandSeparator(number_as_string) {
var parts = number_as_string.split('.');
var integerPart = parts[0];
var decimalPart = parts.length > 1 ? '.' + parts[1] : '';
integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return integerPart + decimalPart;
}

vars.field1 = addThousandSeparator(String(vars.recCount));