c# - string.Format("{0:n0}") in javascript -
this question has answer here:
- format numbers in javascript similar c# 15 answers
i'm trying find equivalent function to:
string.format("{0:n0}")
in javascript.
or in different words, have long number 10898502 , want display 10,898,502.
is there easy way ?
thanks,
for pure javascript solution, recommend function below. courtesy of community wiki entry: how can format numbers money in javascript?
number.prototype.formatmoney = function(c, d, t){ var n = this, c = isnan(c = math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", = parseint(n = math.abs(+n || 0).tofixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + math.abs(n - i).tofixed(c).slice(2) : ""); }; console.log( (1000000.94).formatmoney(1, '.', ',') // 1,000,000.9 );
Comments
Post a Comment