DEV Community

@kon_yu
@kon_yu

Posted on

How to create a string of numbers with , $, €, or £ in JavaScript.

To display a string of money in JavaScript with a comma, use the toLocaleString method.

ref: toLocaleString specification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

Japanese Yen

const price = 1234567890.1234
const jpy = price.toLocaleString('JP',
  { style: 'currency', currency: 'JPY' })

console.log(jpy)
// => ¥1,234,567,890
Enter fullscreen mode Exit fullscreen mode

U.S. dollar

const price = 1234567890.1234
const usd = price.toLocaleString('US',
  { style: 'currency', currency: 'USD',})

console.log(usd)
// => $1,234,567,890.12"
Enter fullscreen mode Exit fullscreen mode

Euro

const price = 1234567890.1234
const eur = price.toLocaleString('DE',
  { style: 'currency', currency: 'EUR' })
console.log(eur)
//=> 1.234.567.890,12 €
Enter fullscreen mode Exit fullscreen mode

British pound

const pond = price.toLocaleString('GB',
  { style: 'currency', currency: 'GBP',})
console.log(pond)
//=> "£1,234,567,890.12"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)