【问题标题】:Angular pipe formatting for millions数百万的角度管道格式
【发布时间】:2018-01-11 17:52:12
【问题描述】:

我正在处理我现在这样格式化的大数字

{{total  | number:'1.0-0'}}

结果是 45,986,592。 我需要将数字格式化为只有数百万,然后只有一个数字,所以45.9

我怎样才能做到这一点?

【问题讨论】:

  • 你不能将总数除以 1,000,000 并用一位十进制数字格式化吗?

标签: angular pipe


【解决方案1】:

如果您想重用此逻辑,您可以构建自定义管道。

按照 Phil 的建议,您可以将其除以 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多