DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
@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

U.S. dollar

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

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

Euro

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

British pound

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

Top comments (0)

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!