@Pipe({
name: 'myPipe'
})
export class MyPipePipe implements PipeTransform {
transform(size: number, extension: string = '%'): any {
return this.toNumberFormat(size) + extension;
}
public toNumberFormat(x) {
let formatNumberValue = parseFloat(x);
if (isNaN(formatNumberValue)) {
return;
}
formatNumberValue = Math.round(x * 100) / 100;
return formatNumberValue;
}
}
使用:
<span>{{numberValue | myPipe}}</span>
这样得到的值都是除100加"%"号,并且保留2位小数.