JS数字格式化函数
关键字: number format
前几天在blog里贴了个JS货币格式化的函数,今天在Matt Snider的网站上淘到了另一个不错的东西,不过和之前说的那个函数有点差别,这个函数是用来格式化数字类型的,很好,很实用。
ps:Matt Snider的网站正如其title一样——Matt Snider JavaScript Resource,内容相当多,个个都比较实用,我喜欢。
/**
* 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);
};
分享到:
相关推荐
该文档为javascript的数字格式化方法,可用于保留小数位,强制添0等操作
JavaScript数字格式化函数,很好用的
主要是对javascript中数字格式化输出的实现代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
Javascript中经常要用到的5种格式化数字的代码,方便大家使用
fmt:——对数字对行格式化 在js中———- Javascript也提供了对数字进行格式化输出的支持 Number对象提供的几种格式化函数: toExponential([fractionDigits]) :将数字按科学计数法格式返回,其中的fractionDigits...
finereport格式化金额函数js中进行数字超大金额千位符格式化处理.docx
currencyFormatter.js是一款简单实用的纯js格式化货币插件。该插件包含155种不同国家的货币,以及715种不同语言的本地化设置。它还能处理某些不采用的货币,功能非常强大。
javascript数字格式化为货币字符,数字格式化为大写货币函数
jquery-number-divider是一款jquery数字格式化分隔符插件。该数字格式化插件可以按指定格式对大数字进行分隔,可以指定分隔符,是一款简单实用的数字格式化...可以通过bower来安装number-divider.js数字格式化插件。
一个用于格式化和处理数字的javascript库。 Travis建立状态 掌握 发展 NPM 贡献 有关贡献信息,请参见CONTRIBUTING.md文件。 语言能力 语言标签遵循规范。 没有单元测试,语言翻译将不会合并。 有关示例,请参见。...
将浮点型数用美元符号和货币符号输出,用格式化的方式。还包括浮点型数据相除时,格式化输出结果。
NULL 博文链接:https://anole1982.iteye.com/blog/1855800
提供了javascript格式化数字的方法,非常简单易用
wnumb.js是一个前端格式化数字金额的插件,比如常见的数字千分位问题。用法如下: // 定义格式化的规则 var moneyFormat = wNumb({ mark: '.', thousand: ',', prefix: '$ ', suffix: ' p.p.' }); // 格式化一...
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) //...
Numeral.js一个用于格式化和操作数字的JavaScript库
accounting.js 是一个非常小的JavaScript方法库用于对数字,金额和货币进行格式化。并提供可选的Excel风格列渲染。它没有依赖任何JS框架。货币符号等可以按需求进行定制
js 写的string format函数,功能模仿C#中的string.Format方法,已实现(整数 :D、小数:F、货币数字:C、科学计数:E 等4种)数字格式化。参数可以传递数组,也可以传多个参数。
运用JAVAScript自定义函数,实现数字字符的格式化转换,在实际网站开发中,有很多地方可以适用,不难却很实用。