11

We are trying to use localization support in our application and looking at the toLocaleString(). We also had a look at Intl.NumberFormat and its format method.

Do they have any relation? Which one is better to use?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
TypeScripter
  • 879
  • 2
  • 10
  • 23
  • Thank you for replying back. yes, I read both of them and I can see that via both ways I can achieve almost similar tasks. However, I am still trying to find which one is better than other (in other words, which one I should prefer and why). I also saw a minimal reference of Intl namespace in localeString doc, but didn't quite grasp how they are related. So, looking for some real world examples. – TypeScripter Sep 28 '16 at 15:44

1 Answers1

15

From MDN docs for Number.prototype.toLocaleString():

Performance:

When formatting large numbers of numbers, it is better to create a NumberFormat object and use the function provided by its NumberFormat.format property.

In most cases you can use Number.prototype.toLocaleString(). It's simpler to use, because it allows you to format a number using one function call. Intl.NumberFormat is useful only when you want to format a large amount of numbers.

Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • 1
    `Intl.NumberFormat` is also useful when you need to pass a formatter function, such as when the format is determined at **run-time** based on a configuration file or user input. – Dem Pilafian Oct 01 '21 at 00:47