JavaScript: Create a specified currency formatting from a given number

JavaScript fundamental (ES6 Syntax): Exercise-126 with Solution

Write a JavaScript program to create a specified currency format from a given number.

  • Use Intl.NumberFormat to enable country / currency sensitive formatting.

Sample Solution:

JavaScript Code:

//#Source https://bit.ly/2neWfJ2 
const toCurrency = (n, curr, LanguageFormat = undefined) =>
  Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n);

console.log(toCurrency(123456.789, 'EUR')); // currency: Euro | currencyLangFormat: Local
console.log(toCurrency(123456.789, 'USD', 'en-us')); // currency: US Dollar | currencyLangFormat: English (United States)
console.log(toCurrency(123456.789, 'USD', 'fa')); //currency: US Dollar | currencyLangFormat: Farsi
console.log(toCurrency(322342436423.2435, 'JPY')); //currency: Japanese Yen | currencyLangFormat: Local
console.log(toCurrency(322342436423.2435, 'JPY', 'fi')); //currency: Japanese Yen | currencyLangFormat: Finnish

Sample Output:

322 342 436 423 ¥


flowchart: Create a specified currency formatting from a given number

Live Demo:

See the Pen javascript-basic-exercise-126-1 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to convert a float-point arithmetic to the Decimal mark form and It will make a comma separated string from a number.
Next: Write a JavaScript program to iterate over a callback n times.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.