【发布时间】:2020-03-10 17:52:01
【问题描述】:
我从 ASP.NET Core API 获取 Km 范围,并希望使用 1000 分隔符格式化数字,例如: 75,000 - 100,000 - 125,000 以此类推。 我如何通过以下方式实现这一目标:
JSON 输出:
[
{
"Id": 8,
"Km": 75000
},
{
"Id": 9,
"Km": 100000
},
{
"Id": 10,
"Km": 125000
},
{
"Id": 11,
"Km": 150000
}
]
我的知识管理模型
export class KmRange {
Id: number;
Km: number;
}
我的组件:
dropdownList = [];
this.dropdownList = await this._service.getKmRanges().toPromise();
编辑 - 我现在就这样使用它
I cam accross this post
它说正则表达式比 toLocaleString 快,所以我最终这样使用。
this.dropdownList = await this._service.getKmRanges().toPromise();
for (var i = 0; i < this.dropdownList.length; i++) {
this.dropdownList[i].Km = this.toCommas(this.dropdownList[i].Km);
}
toCommas(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
【问题讨论】:
标签: typescript asp.net-core angular9