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!
Flashy FAQs Using MooTools Sliders
I often qualify a great website by one that pay attention to detail and makes all of the “little things” seem as though much time was spent on them. Let’s face it — FAQs are as boring as they come. That is, until you…
Implement jQuery’s hover() Method in MooTools
jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here’s how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other…
Source link