Intl.NumberFormat
Formatting numbers on the client side is an important task, especially when you consider how much raw API usage is in play these days. What’s also important is ensuring those numbers are meaningful to users, no matter where they are in the world, especially if you’re an eCommerce website.
Writing internationalization code can be a nightmare but luckily JavaScript provides us Intl.NumberFormat
, an API for internationalizing numbers as currencies and more. Let’s check it out!
Some examples of Intl.NumberFormat
include:
new Intl.NumberFormat().format(12345) // 12,345 new Intl.NumberFormat('en-US', { maximumSignificantDigits: 4}).format(1.2345678) // 1.235 (Notice the rounding) new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP' }).format(9002.20) // £9,002.20 new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(9002.20) // 9.002,20 € new Intl.NumberFormat().formatToParts(12345.678) /* [ { "type":"integer", "value":"12" }, { "type":"group", "value":"," }, { "type":"integer", "value":"345" }, { "type":"decimal", "value":"." }, { "type":"fraction", "value":"678" } ] */
Don’t bother writing your own client side number formatting functions if the numbers you want to present are standard formats — leverage the amazing APIs the browser provides you!
fetch API
One of the worst kept secrets about AJAX on the web is that the underlying API for it,
XMLHttpRequest
, wasn’t really made for what we’ve been using it for. We’ve done well to create elegant APIs around XHR but we know we can do better. Our effort to…Send Text Messages with PHP
Kids these days, I tell ya. All they care about is the technology. The video games. The bottled water. Oh, and the texting, always the texting. Back in my day, all we had was…OK, I had all of these things too. But I still don’t get…
[ad_2]
Source link