0

i'm using ejs for very small homepage

I wanna insert comma every three digit

Now situation

<%= product.price %> 
result = 1000000

I want like this

<%= product.price %> // some function
result 1,000,000

can you help me?

SILENMUS
  • 709
  • 1
  • 10
  • 25
  • Does this answer your question? [How to print a number with commas as thousands separators in JavaScript](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) – Caleb Denio Feb 10 '22 at 04:28
  • yeah this is the one , but don't know how to use it in ejs – SILENMUS Feb 10 '22 at 04:39
  • thanks dude you gave me clue to solve it – SILENMUS Feb 10 '22 at 05:07

1 Answers1

0

self answer

just pass the function to ejs

[ express ]

res.render(`${__dirname}/views/dashboard.ejs`, {
   ✅ numberWithCommas ✅ : function (x) {
     return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
   },
});

[ ejs ]
now you can call the function 

<%- numberWithCommas(product.price) %>

result
1,000,000
SILENMUS
  • 709
  • 1
  • 10
  • 25