标题非常概括我的需求。Angular:使用逗号格式数字
123456789 => 123,456,789
12345 => 12,345
什么是获得此转换的最佳方法?不要在Angular-2中建议货币管道,因为我不需要$或货币符号来预先输出我的输出。
标题非常概括我的需求。Angular:使用逗号格式数字
123456789 => 123,456,789
12345 => 12,345
什么是获得此转换的最佳方法?不要在Angular-2中建议货币管道,因为我不需要$或货币符号来预先输出我的输出。
使用DecimalPipe像可在https://angular.io/api/common/DecimalPipe
不使用管道,一个简单的方法来回答的JavaScript您的问题,这
{{attr | number}}
Working Plunker
文档:
var str = "The quick brown fox jumps over the lazy dogs.".replace(/(.{2})/g,"$1,");
而且这将输出Th,e ,qu,ic,k ,br,ow,n ,fo,x ,ju,mp,s ,ov,er, t,he, l,az,y ,do,gs,.
但我认为你制定你的问题不好,所以,如果你想分析的数字,你应该使用这个功能:
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
所以
var num = numberWithCommas(1234567);
console.log(num);
这将输出1,234,567
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function printNo() {
document.getElementById('text').innerHTML =
Number(1234355).toLocaleString('en-GB');
}
</script>
</head>
<body onload="printNo()">
<h1 id="text"></h1>
</body>
</html>
https://plnkr.co/edit/OU0koo2r1YuKC1rC3tpN?p=preview https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
可能重复的[如何在JavaScript中使用逗号作为千位分隔符来打印数字](https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as千万分隔符在JavaScript) – gforce301
@ gforce301这是一个不同的环境,因为CodeWarrior用管道解决方案来回答,所以我们也想为angular2提供解决方案。 –