如何自定义数字管角2

时间:2016-08-12 16:27:07

标签: angular typescript

我想自定义数字管角2 {{myvar |数:' 1.2-2' }} 对于myvar = 1000 我得到1000 我想要的是获得1 000 代替的空间, 任何想法?

2 个答案:

答案 0 :(得分:0)

我从How to print a number with commas as thousands separators in JavaScript找到了这个解决方案。它的工作原理!



function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}




答案 1 :(得分:0)

你可以链管。 {{ myvar | number:'1.2-2' | thousand }}和你的千管道看起来像这样。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'thousand'
})

export class ThousandPipe implements PipeTransform {
  transform(value: any, digits?: string): string {
    if (value) {
      let thousands = value.split(',');
      const preDecimalValue = thousands.pop();
      thousands = thousands.join(' ');
      return thousands + ' ' + preDecimalValue;
    }
    return '';
  }
}

假设所有数千个都以逗号分隔,您将设置数千个数组中的所有数千个。在您的情况下,成千上万将是['1'],而preDecimalValue将是'000.00'