如何格式化干净的数字,使 1000 显示为 1.000,00
How to format clean numbers so 1000 appear as 1.000,00
我目前有一些干净的数字,例如: 1, 10, 100, 1000, 10000
等...
我希望格式化这些数字,因此它们显示为:
1,00
10,00
100,00
1.000,00
10.000,00
我玩了n.toFixed(2);
但似乎不支持我寻找的格式。
有什么想法吗?
查看 Intl.NumberFormat((,不需要花哨的裤子插件。跨平台支持。
var number = 3500;
alert(new Intl.NumberFormat().format(number));
将弹出"3,500">
如果你想要一个简单的函数来完成这项工作,以下可能适合:
// Format a number n using:
// p decimal places (two by default)
// ts as the thousands separator (comma by default) and
// dp as the decimal point (period by default).
//
// If p < 0 or p > 20 results are implementation dependent.
function formatNumber(n, p, ts, dp) {
var t = [];
// Get arguments, set defaults
if (typeof p == 'undefined') p = 2;
if (typeof ts == 'undefined') ts = ',';
if (typeof dp == 'undefined') dp = '.';
// Get number and decimal part of n
n = Number(n).toFixed(p).split('.');
// Add thousands separator and decimal point (if requied):
for (var iLen = n[0].length, i = iLen? iLen % 3 || 3 : 0, j = 0; i <= iLen; i+=3) {
t.push(n[0].substring(j, i));
j = i;
}
// Insert separators and return result
return t.join(ts) + (n[1]? dp + n[1] : '');
}
//*
console.log(formatNumber(
1234567890.567, // value to format
4, // number of decimal places
'.', // thousands separator
',' // decimal separator
)); // result: 1.234.567.890,5670
//*/
console.log(formatNumber(
123.567, // value to format
1 // number of decimal places
)); // result: 123.6
console.log(formatNumber(
'123.567', // value to format
0 // number of decimal places
)); // result: 123.6
console.log(formatNumber(
123, // value to format
0 // number of decimal places
)); // result: 123
console.log(formatNumber(
13, // value to format
2 // number of decimal places
)); // result: 13.00
console.log(formatNumber(
0 // value to format
// number of decimal places
)); // result: 0.00
console.log(formatNumber(
// value to format
// number of decimal places
)); // result: NaN
抱歉,没有花哨的正则表达式或切片/拼接数组的东西,只有有效的 POJS。
你可以
这样做:
var nb='1000000000';
var result = nb.replace(/(?:(^'d{1,3})(?=(?:'d{3})*$)|('d{3}))(?!$)/mg, '$1$2.')+',00';
在 JS 中没有用于格式化数字的内置方法。您可以为此使用自动数字。http://www.decorplanit.com/plugin/
我不知道
你的情况是什么,但我想使用angularJS。
它完全符合您的需求,并且还有更多额外的动态格式。
更新:专门研究本地化功能。如果您需要,它甚至可以提供多元化服务。
相关文章:
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何在生成下载文件时显示加载动画
- React重新渲染但未显示正确的组件
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 音频控件在mouseover上显示,在mouseout上淡出
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 显示5秒后隐藏潜水
- 画廊图像未显示
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 仅当屏幕上显示 <1000 像素时调用 JS 函数
- 如何格式化干净的数字,使 1000 显示为 1.000,00
- 如何显示只有100行1000从数据库与选项去下一个100和前100在html/php/css/js
- 字符串"1000"并且我需要将其显示为"10.00"/10.00(字符串或数字)
- 在性能方面,在一个页面上显示1000张图片的最佳方法是什么?