Formatting Numbers Using toLocaleString() In JavaScript
I've been using a JavaScript 3rd party library to format numbers with comma and decimal place(s). The JavaScript library called NumberFormatter was introduced to me by my developer with exceptional skills in front-end programming. Due to simplicity sake, I'm exploring options if this can be achieved using an existing JavaScript function instead of using the existing 3rd party app.
After doing some research, I found an answer here: How to use toLocaleString() and tofixed(2) in javascript which is to use toLocaleString() by setting the options minimumFractionDigits and maximumFractionDigits to 2.
Number.prototype.toLocaleFixed = function (n) { return this.toLocaleString(undefined, { minimumFractionDigits: n, maximumFractionDigits: n }); };
var result = parseFloat(amount).toLocaleFixed(2)
Comments
Post a Comment