js number to currency code example

Example 1: tofixed currency in js

(12345.67).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');  // 12,345.67

Example 2: javascript get currency symbol by currencyCode

var str1 = '10,00 fr';
var str2 = '12.22 $';

function getCurrencySymbol(str) {
  //replace all numbers, spaces, commas, and periods with an empty string
  //we should only be left with the currency symbols
  return str.replace(/[\d\., ]/g, '');
}

console.log(getCurrencySymbol(str1));
console.log(getCurrencySymbol(str2));

Example 3: javascript get currency symbol by currencyCode

var str1 = '10,00 €';
var str2 = '12.22 $';

function getCurrencySymbol(str) {
  //replace all numbers, spaces, commas, and periods with an empty string
  //we should only be left with the currency symbols
  return str.replace(/[\d\., ]/g, '');
}

console.log(getCurrencySymbol(str1));
console.log(getCurrencySymbol(str2));