Friday, December 2, 2022
HomeSoftware DevelopmentMethods to Internationalize Numbers with JavaScript

Methods to Internationalize Numbers with JavaScript


Presenting numbers in a readable format takes many types, from visible charts to easily including punctuation. These punctuation, nonetheless, are completely different primarily based on internationalization. Some international locations use , for decimal, whereas others use .. Apprehensive about having to code for all this insanity? Do not — JavaScript gives a way do the onerous give you the results you want!

The Quantity primitive has a toLocaleString technique to do the fundamental formatting for you:

const value = 16601.91;

// Fundamental decimal format, no offering locale
// Makes use of locale offered by browser since none outlined
value.toLocaleString(); // "16,601.91"

// Present a particular locale
value.toLocaleString('de-DE'); // "16.601,91"

// Formatting foreign money is feasible
value.toLocaleString('de-DE', { 
  fashion: 'foreign money', 
  foreign money: 'EUR' 
}); // "16.601,91 €"

// You too can use Intl.NumberFormat for formatting
new Intl.NumberFormat('en-US', {
  fashion: 'foreign money',
  foreign money: 'EUR'
}).format(value); // £16,601.91

It is a main aid that JavaScript gives us these kind of helpers in order that we need not depend on bloated third-party libraries. No excuses — the device is there!

  • Responsive Images: The Ultimate Guide

    Likelihood is that any Internet designers utilizing our Ghostlab browser testing app, which permits seamless testing throughout all gadgets concurrently, could have labored with responsive design in some form or type. And as right this moment’s web sites and gadgets develop into ever extra diversified, a plethora of responsive photos…

  • 9 More Mind-Blowing WebGL Demos

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments