我正在使用大数字,我现在正在格式化这个
{{total | number:'1.0-0'}}
结果是45,986,592。
我需要将数字格式化为只有数百万,然后只需一位数,所以45.9
我怎样才能做到这一点?
答案 0 :(得分:2)
如果要重用此逻辑,可以构建自定义管道。
菲尔建议,你可以把它除以1,000,000
import { Pipe, PipeTransform } from '@angular/core';
import { DecimalPipe } from '@angular/common';
@Pipe({
name: 'million'
})
export class MillionPipe implements PipeTransform {
constructor(private decimalPipe: DecimalPipe) {
}
transform(value: any, digits?: any): any {
return this.decimalPipe.transform(value/1000000, digits)
}
}
确保模块提供程序中有小数点管道,以便管道可以注入它。
工作演示:
https://stackblitz.com/edit/angular-aa16so?file=app%2Fmillion.pipe.ts