Currency formatting

by Julien Roche

HTML

<code id="usd"></code>
<br />
<code id="eur"></code>
<br />
<code id="yen"></code>
<br />
<code id="cny"></code>

TypeScript

// See https://twitter.com/wesbos/status/966697179904651264
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
const price = 100.42;
const currencies = [
	{ 'locale': 'en-US', 'currency': 'USD' },
  { 'locale': 'de-DE', 'currency': 'EUR' },
  { 'locale': 'ja-JP', 'currency': 'YEN' },
  { 'locale': 'zh-Hans-CN', 'currency': 'CNY' }
];

currencies.forEach(({ locale, currency }) => {
	let element = document.getElementById(currency.toLowerCase());
  let localeConf = { 'style': 'currency', currency };
	element.innerText = price.toLocaleString(locale, localeConf);
});