如何使用 jQuery 格式化数字
how to format number the using jquery
我正在尝试删除"."之后的数字,然后我想将数字(16810900.211233)格式化为像这样(16,810,900),但我不知道该怎么做,这是我的html是这样的:
<link rel="stylesheet" type="text/css" href="http://cdn.webrupee.com/font" />
<div class="main">
<p class="active">10200.00</p>
<p class="in_active">16810900.211233</p>
<p class="active">0</p>
<p class="in_active">-</p>
<p class="active">1278200.543554</p>
<p class="in_active">-</p>
<p class="active">-</p>
<p class="in_active">9,890.656</p>
<p class="active">10,200</p>
<p class="in_active">16810900</p>
<p class="active">0</p>
<p class="in_active">-</p>
<p class="active">1278200.09</p>
<p class="in_active">-</p>
<p class="active">-</p>
<p class="in_active">9890.00</p>
</div>
我在这里试过 jsfiddle
从 http://ntt.cc/2008/04/25/6-very-basic-but-very-useful-javascript-number-format-functions-for-web-developers.html 撕下来
重新发明轮子没有意义.. :)
/**
* 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 precision
if (1 < psplit.length) {
// fix number precision
that = that.toFixed(psplit[1].length);
}
// error: too many periods
else if (2 < psplit.length) {
throw(’NumberFormatException: invalid format, formats should have no more than 1 period: ‘ + format);
}
// remove precision
else {
that = that.toFixed(0);
}
// get the string now that precision is correct
var fnum = that.toString();
// format has comma, then compute commas
if (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 3
for (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 in
if (psplit[1]) {fnum += ‘.’ + psplit[1];}
}
// replace the number portion of the format with fnum
return format.replace(/['d,?'.?]+/, fnum);
};
全球化库是实现目标的最简单方法。 这允许您指定格式并说明不同区域性处理数字分组的方式。 例如:
Globalize.format(Math.round(16810900.211233))
结果16,810,900
$("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 characters
var group = 3;
var min = v.length > 4 ? group : 4; // format thousands without separator
for (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(".");
});
即使没有为 4 个字符的数字设置千位分隔符,这也将格式化数字。
相关文章:
- 如何在谷歌可视化中组合数字和模式格式化程序
- 使用纯javascript格式化数字
- 用javascript格式化数字,而不进行更改
- 如何用javascript格式化(大)数字
- 使用角度数字格式过滤器格式化值
- Java脚本插件,用于将数字转换为特定于区域性的货币格式,并将具有特定货币格式的非格式化数字转换为普通数字
- 如何在angularjs中对数字进行复数和格式化
- jQuery(或Javascript)基于给定的C#/VBA/Java格式字符串格式化数字的方法
- 如何在Javascript中格式化(真正的)大数字
- 使用数字OnfocusOut/OnBlur格式化文本框
- 格式化数字,如22,55,86,21,28
- Javascript-格式化数字,始终显示原始的小数位数
- 正则表达式:使用 JavaScript 从字符串中获取格式化的数字 n.n.n
- 使用 javascript 格式化数字
- Chrome告诉“Invaild Group”使用正则表达式来格式化数字
- 在 javascript 中用指数格式化数字
- 数字格式化使用javascript
- 数字格式化,每3位空格
- 模板字符串中的数字格式化(Javascript - ES6)
- Javascript数字格式化