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: 'EUR' ).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!
CSS vs. JS Animation: Which is Faster?
How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point…
5 HTML5 APIs You Didn’t Know Existed
When you say or read “HTML5”, you half expect exotic dancers and unicorns to walk into the room to the tune of “I’m Sexy and I Know It.” Can you blame us though? We watched the fundamental APIs stagnate for so long that a basic feature…
CSS Custom Cursors
Remember the Web 1.0 days where you had to customize your site in every way possible? You abused the scrollbars in Internet Explorer, of course, but the most popular external service I can remember was CometCursor. CometCursor let you create and use loads of custom cursors for…
Fancy FAQs with jQuery Sliders
Frequently asked questions can be super boring, right? They don’t have to be! I’ve already shown you how to create fancy FAQs with MooTools — here’s how to create the same effect using jQuery. The HTML Simply a series of H3s and DIVs wrapper…
Source link