thousands separator in angular 4

1

I'm using angle 4 and I want to be able to use a thousands separator pipe for example if the amount is 6000 that appears 6,000, if the amount is 60000 that appears 60,000 and so ...

I tried to use some pipes but I can not find one that works for me, to show the amount I use a

 ngFor="let item of items" 

and in the part that I show the amount I put

{{item.Monto/1000 | currency:'USD':true:'2.3-3'}}

with that I see the amount as 06.000 instead of 6.000

    
asked by felipe caro 07.02.2018 в 18:22
source

1 answer

1

You can use the following Pipe :

  

Every 3 digits insert a "." (period) with the regular expression

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

    @Pipe({
        name: 'thousandsPipe'
    })

export class ThousandsPipe implements PipeTransform {

    public transform(value: any) {
        return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");;
    }
}

In HTML :

{{item.Monto/1000 | thousandsPipe}}
  

Important fact, this only adds the separation of thousands, not the decimal or the currency.

Additional Information:

As for the Angular own Pipe that you use, it works in the following way:

variable_numero | number[:digitInfo]

digitInfo is constituted as follows:

{cantMinDigitosEnteros}.{cantMinDigitosDecimales}-{cantMaxDigitosDecimales}
  

CurrencyPipe , and DecimalPipe , fall on the same operation, vary in the possibility of inserting the currency type and what that implies.

    
answered by 07.02.2018 в 19:09