How to convert a number to currency format by Javascript

This function below will convert your number to currency format easily

Write a function call currencyFormat with below code :

 function currencyFormat(value) {
  value = Number(value);
  if (!Number.isInteger(value)) {
    value = value.toFixed(2);
  }
  return value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}


Usage:

 const myMoney = currencyFormat(100000.8546);
// myMoney = 100,000.85


javascript

© 2019 4codev

Created with love by Sil.