Formatting Number In Javascript Without Using Minimumfractiondigits, Maximumfractiondigits
I have following types of values in a, b, c, d. a= 12345678 b= 12345678.098 c=12345678.1 d=12345678.11 I need to format like, a = 12,345,678.000 b= 12,345,678.098 c=12,345,678.10
Solution 1:
This might help you.
var a = 12345678;
var b = 12345678.098;
var c = 12345678.1;
var d = 12345678.11;
String.prototype.format = function() {
returnthis.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
};
functionformatNumber(input) {
returnparseFloat(input).toFixed(3).toString().format();
}
console.log(formatNumber(a));
console.log(formatNumber(b));
console.log(formatNumber(c));
console.log(formatNumber(d));
Post a Comment for "Formatting Number In Javascript Without Using Minimumfractiondigits, Maximumfractiondigits"