How to Internationalize Numbers with JavaScript

Presenting numbers in a readable format takes many forms, from visual charts to simply adding punctuation. Those punctuation, however, are different based on internationalization. Some countries use , for decimal, while others use .. Worried about having to code for all this madness? Don’t — JavaScript provides a method do the hard work for you! […]

The post How to Internationalize Numbers with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Presenting numbers in a readable format takes many forms, from visual charts to simply adding punctuation. Those punctuation, however, are different based on internationalization. Some countries use , for decimal, while others use .. Worried about having to code for all this madness? Don’t — JavaScript provides a method do the hard work for you!

The Number primitive has a toLocaleString method to do the basic formatting for you:

const price = 16601.91;

// Basic decimal format, no providing locale
// Uses locale provided by browser since none defined
price.toLocaleString(); // "16,601.91"

// Provide a specific locale
price.toLocaleString('de-DE'); // "16.601,91"

// Formatting currency is possible
price.toLocaleString('de-DE', { 
  style: 'currency', 
  currency: 'EUR' 
}); // "16.601,91 €"

// You can also use Intl.NumberFormat for formatting
new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'GBP'
}).format(price); // £16,601.91

It’s a major relief that JavaScript provides us these type of helpers so that we don’t need to rely on bloated third-party libraries. No excuses — the tool is there!

The post How to Internationalize Numbers with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2022-11-21T12:40:22+00:00) How to Internationalize Numbers with JavaScript. Retrieved from https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/

MLA
" » How to Internationalize Numbers with JavaScript." David Walsh | Sciencx - Monday November 21, 2022, https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/
HARVARD
David Walsh | Sciencx Monday November 21, 2022 » How to Internationalize Numbers with JavaScript., viewed ,<https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/>
VANCOUVER
David Walsh | Sciencx - » How to Internationalize Numbers with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/
CHICAGO
" » How to Internationalize Numbers with JavaScript." David Walsh | Sciencx - Accessed . https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/
IEEE
" » How to Internationalize Numbers with JavaScript." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/. [Accessed: ]
rf:citation
» How to Internationalize Numbers with JavaScript | David Walsh | Sciencx | https://www.scien.cx/2022/11/21/how-to-internationalize-numbers-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.