How To Format Number The Using Jquery
I am trying to remove the number which are coming after '.' then i want to format the number (16810900.211233) to like this (16,810,900) but i dont know how to do that here is my
Solution 1:
No point in reinventing the wheel.. :)
/**
* Formats the number according to the ‘format’ string;
* adherses to the american number standard where a comma
* is inserted after every 3 digits.
* note: there should be only 1 contiguous number in the format,
* where a number consists of digits, period, and commas
* any other characters can be wrapped around this number, including ‘$’, ‘%’, or text
* examples (123456.789):
* ‘0′ - (123456) show only digits, no precision
* ‘0.00′ - (123456.78) show only digits, 2 precision
* ‘0.0000′ - (123456.7890) show only digits, 4 precision
* ‘0,000′ - (123,456) show comma and digits, no precision
* ‘0,000.00′ - (123,456.78) show comma and digits, 2 precision
* ‘0,0.00′ - (123,456.78) shortcut method, show comma and digits, 2 precision
*
* @method format
* @param format {string} the way you would like to format this text
* @return {string} the formatted number
* @public
*/Number.prototype.format = function(format) {
if (! isType(format, ’string’)) {return ”;} // sanity check var hasComma =-1< format.indexOf(’,'),
psplit = format.stripNonNumeric().split(’.'),
that = this;
// compute precisionif (1< psplit.length) {
// fix number precision
that = that.toFixed(psplit[1].length);
}
// error: too many periodselseif (2< psplit.length) {
throw(’NumberFormatException: invalid format, formats should have no more than 1 period: ‘ + format);
}
// remove precisionelse {
that = that.toFixed(0);
}
// get the string now that precision is correctvar fnum = that.toString();
// format has comma, then compute commasif (hasComma) {
// remove precision for computation
psplit = fnum.split(’.');
var cnum = psplit[0],
parr = [],
j = cnum.length,
m =Math.floor(j /3),
n = cnum.length %3||3; // n cannot be ZERO or causes infinite loop // break the number into chunks of 3 digits; first chunk may be less than 3for (var i =0; i < j; i += n) {
if (i !=0) {n =3;}
parr[parr.length] = cnum.substr(i, n);
m -=1;
}
// put chunks back together, separated by comma
fnum = parr.join(’,');
// add the precision back inif (psplit[1]) {fnum += ‘.’ + psplit[1];}
}
// replace the number portion of the format with fnumreturn format.replace(/[\d,?\.?]+/, fnum);
};
Solution 2:
The Globalize library is the simplest way to accomplish your goal. This allows you to specify a format and account for the way that different cultures handle number grouping. For example:
Globalize.format(Math.round(16810900.211233))
results in 16,810,900
Solution 3:
$("p").text(function(i, v) {
if (v == "-")
return v; // or whatever you want to to with these
v = v.split(".")[0]; // remove anything after the first "."
v = v.replace(/[^\d]/g, ""); // remove non-numeric charactersvargroup = 3;
var min = v.length > 4 ? group : 4; // format thousands without separatorfor (var res=[], i=v.length; i>min; i-=group)
res.push(v.substr(i-group, group));
res.push(v.substr(0,i));
return res.reverse().join(".");
});
This will format numbers even with the thousand separator not set for 4-character-numbers.
Post a Comment for "How To Format Number The Using Jquery"